How to check ports from client to coreserver with five(9)s Console Custom Button?

Modified on Wed, 10 May 2023 at 01:40 PM

Very often we can not really be sure if all the necessary IP ports are open from client to core (and back).

Here is a tutorial zu add these functionality into five(9)s console with an powershell custom button

1st of all: You need admin rights in five(9)s console.

open Adminpage => General Settings => Custom Buttons

Add custom button:
Give it a name as you like:
For Example: Port Client to Core
Select Type: PowerShell
Select location: Selected Device
Copy the following Powershell script and paste it into PowerShell content section


Here is the required Powershellscript (Client to Core)       
$ip = "<youre coreserver name>" 
$Protocol = 3 
$Message = "PortCheck from: %Hostname% <b>  ==>    </b>" + $ip + "<br><br>" 
 
if (!(Test-Path -path "C:\LDPorts")) {New-Item "C:\LDPorts" -Type Directory} 
 
$datestring = (Get-Date).ToString("s").Replace(":",".") 
 
function Test-Port{  
 
[cmdletbinding(  
    DefaultParameterSetName = '',  
    ConfirmImpact = 'low'  
)]  
    Param(  
        [Parameter(  
            Mandatory = $True,  
            Position = 0,  
            ParameterSetName = '',  
            ValueFromPipeline = $True)]  
            [array]$computer,  
        [Parameter(  
            Position = 1,  
            Mandatory = $True,  
            ParameterSetName = '')]  
            [array]$port,  
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [int]$TCPtimeout=1000,  
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [int]$UDPtimeout=1000,              
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [switch]$TCP,  
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [switch]$UDP                                    
        )  
    Begin {  
        If (!$tcp -AND !$udp) {$tcp = $True}  
        $ErrorActionPreference = "SilentlyContinue"  
        $report = @() 
        $report | Format-List 
    }  
    Process {      
        ForEach ($c in $computer) {  
            ForEach ($p in $port) {  
                If ($tcp) 
{    
                    $temp = "" | Select Port, Open 
                    $tcpobject = new-Object system.Net.Sockets.TcpClient  
                    $connect = $tcpobject.BeginConnect($c,$p,$null,$null)  
                    $wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false)  
                    If(!$wait) 
{  
                        $tcpobject.Close()  
                        Write-Verbose "Connection Timeout"  
                        $temp.Port = $p  
                        $temp.Open = "Closed or filtered"  
                    } Else 
                        {  
                    try 
                    { 
                         $error.Clear()  
                        $tcpobject.EndConnect($connect) | out-Null    
                    } 
                    catch 
                    { 
                            If($error[0]){  
                            [string]$string = ($error[0].exception).message  
                            $message = (($string.split(":")[1]).replace('"',"")).TrimStart()  
                            $failed = $true  
                        }  
                    }                  
                       $tcpobject.Close()  
                        If($failed) 
{  
                            $temp.Port = $p    
                            $temp.Open = "Closed or filtered"  
                        } Else{  
                            $temp.Port = $p 
                            $temp.Open = "Open"    
                        }  
                    }      
                    $failed = $Null      
                    $report += $temp  
                }      
                If ($udp) {  
                    $temp = "" | Select Port, Open                                    
                    $udpobject = new-Object system.Net.Sockets.Udpclient 
                    $udpobject.client.ReceiveTimeout = $UDPTimeout 
                    $udpobject.Connect("$c",$p) 
                    Write-Verbose "Sending message to remote host" 
                    $a = new-object system.text.asciiencoding 
                    $byte = $a.GetBytes("$(Get-Date)") 
                    [void]$udpobject.Send($byte,$byte.length) 
                    $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) 
                    Try { 
                        Write-Verbose "Waiting for message return" 
                        $receivebytes = $udpobject.Receive([ref]$remoteendpoint) 
                        [string]$returndata = $a.GetString($receivebytes) 
                        If ($returndata) { 
                           $temp.Port = $p 
                           $temp.Open = "Open"  
                           $udpobject.close()    
                        }                        
                    } Catch { 
                        If ($Error[0].ToString() -match "\bRespond after a period of time\b") { 
                            $udpobject.Close()  
                            If (Test-Connection -comp $c -count 1 -quiet) { 
                                $temp.Port = $p 
                                $temp.Open = "Open"  
                            } Else 
{ 
                               $temp.Port = $p  
                               $temp.Open = "Closed or filtered"  
                            }                          
                        } ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) { 
                           $udpobject.Close()  
                            $temp.Port = $p 
                            $temp.Open = "Closed or filtered"  
                        } Else {                      
                            $udpobject.close() 
                        } 
                    }      
                    $report += $temp 
                }                                  
            }  
        }                  
    }  
    End 
{          $report 
    } 
} 
 
function Fulltest() 
{ 
$TCPresults = Test-Port -comp $ip -port 80,137,139,443,445,4343,5007,9594,9595,9982,12175,12176,16992,16993,16994,33354 -tcp -TCPtimeout 1800 
$TCPresults | Out-File $TCPtxt 
$UDPresults = Test-Port -comp $ip -port 67,69,1759,4011,9535,9595,38293 -udp -UDPtimeout 1800 
$UDPresults | Out-File $UDPtxt 
 
 
$textTCP = "<b>=====>  TCP  <=====</b><br>" 
foreach($lineTCP in [System.IO.File]::ReadLines($TCPtxt)) 
{ 
      $textTCP += $lineTCP + "<br>" 
} 
 
$textUDP = "<b>=====>  UDP  <=====</b><br>" 
foreach($lineUDP in [System.IO.File]::ReadLines($UDPtxt)) 
{ 
      $textUDP += $lineUDP + "<br>" 
} 
 
$EXPLORETCP = "<br>Results are on %Hostname% in Folder: \\%Hostname%\c$\LDPorts\$ip\TCP" 
$EXPLOREUCP = "<br>Results are on %Hostname% in Folder: \\%Hostname%\c$\LDPorts\$ip\UCP" 
 
$TCPAnzeige = $Message + $textTCP + $EXPLORETCP 
$UDPAnzeige = $Message + $textUDP + $EXPLOREUCP 
 
PostMessageInfo $TCPAnzeige 
PostMessageInfo $UDPAnzeige 
} 
 
if (!(Test-Path -path "C:\LDPorts\$ip")) {New-Item "C:\LDPorts\$ip" -Type Directory} 
if (!(Test-Path -path "C:\LDPorts\$ip\UDP")) {New-Item "C:\LDPorts\$ip\UDP" -Type Directory} 
if (!(Test-Path -path "C:\LDPorts\$ip\TCP")) {New-Item "C:\LDPorts\$ip\TCP" -Type Directory} 
 
$TCPtxt = "C:\LDPorts\$ip\TCP\TCPportsCheck-$ip-$datestring.txt" 
$UDPtxt = "C:\LDPorts\$ip\UDP\UDPportsCheck-$ip-$datestring.txt" 
 
switch ($Protocol) 
        { 
        1{TCPtest} 
        2{UDPtest} 
        3{Fulltest} 
        } 

It´s not necessary to insert these feedback functions to the script. In this special case the function is allready included.
Last Modified Date
08.05.2023

Verified versions
five(9)s Console version 4.2 or higher


Tags
  • Custom Button
  • Powershell
  • Portcheck  

Disclaimer
Even though every care has been taken by five(9)s GmbH to ensure that the information contained in this publication is correct and complete, it is possible that this is not the case. five(9)s GmbH provides the publication "as is", without any warranty for its soundness, suitability for a different purpose or otherwise. five(9)s GmbH is not liable for any damage which has occurred or may occur as a result of or in any respect related to the use of this publication. five(9)s GmbH may change or terminate this publication at any time without further notice and shall not be responsible for any consequence(s) arising there from. Subject to this disclaimer, five(9)s GmbH is not responsible for any contributions by third parties to this publication.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article