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

Modified on Wed, 10 May 2023 at 01:30 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 them an good name as you like:
For Example: Port Core to Client
Select Type: PowerShell
Select location: Core
Copy the following Powershellscriptand paste into the PowerShell Content Section


Here is the required Powershellscript (Core to Client)
        
$Coreserver = "<your Coreservername>"
$ip = "%Hostname%"
$Protocol = 3

$Message = "PortCheck from $Coreserver<b>  ==>    </b> %Hostname%  <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() 
                        $temp.Port = $p 
                        $temp.Open = "Closed or filtered" 
                    } Else { 
                        $error.Clear() 
                        try
                        {
                            $tcpobject.EndConnect($connect) | out-Null
                        }
                        catch
                        {
                            If($error[0])
                            { 
                                [string]$string = ($error[0].exception).message 
                                $message = (($string.split(":")[1]).replace('"',"")).TrimStart() 
                                $failed = $true 
                            } 
                        }
                        #Close connection     
                        $tcpobject.Close() 
                        #If unable to query port to due failure 
                        If($failed){ 
                            #Build report   
                            #$temp.Server = $c
                            $temp.Port = $p   
                            #$temp.TypePort = "TCP"
                            $temp.Open = "Closed or filtered" 
                            #$temp.Notes = "$message" 
                        } Else{ 
                            #Build report   
                            #$temp.Server = $c
                            $temp.Port = $p
                            #$temp.TypePort = "TCP"   
                            $temp.Open = "Open"   
                        } 
                    }     
                    #Reset failed value 
                    $failed = $Null     
                    #Merge temp array with report             
                    $report += $temp 
                }     
                If ($udp) { 
                    #Create temporary holder $temp = "" | Select Server, Port, TypePort, Open, Notes   
                    $temp = "" | Select Port, Open                                   
                    #Create object for connecting to port on computer 
                    $udpobject = new-Object system.Net.Sockets.Udpclient
                    #Set a timeout on receiving message
                    $udpobject.client.ReceiveTimeout = $UDPTimeout
                    #Connect to remote machine's port               
                    Write-Verbose "Making UDP connection to remote server"
                    $udpobject.Connect("$c",$p)
                    #Sends a message to the host to which you have connected.
                    Write-Verbose "Sending message to remote host"
                    $a = new-object system.text.asciiencoding
                    $byte = $a.GetBytes("$(Get-Date)")
                    [void]$udpobject.Send($byte,$byte.length)
                    #IPEndPoint object will allow us to read datagrams sent from any source. 
                    Write-Verbose "Creating remote endpoint"
                    $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
                    Try {
                        #Blocks until a message returns on this socket from a remote host.
                        Write-Verbose "Waiting for message return"
                        $receivebytes = $udpobject.Receive([ref]$remoteendpoint)
                        [string]$returndata = $a.GetString($receivebytes)
                        If ($returndata) {
                           Write-Verbose "Connection Successful" 
                            #Build report 
                            #$temp.Server = $c
                            $temp.Port = $p
                            #$temp.TypePort = "UDP"   
                            $temp.Open = "Open" 
                            #$temp.Notes = $returndata   
                            $udpobject.close()   
                        }                       
                    } Catch {
                        If ($Error[0].ToString() -match "\bRespond after a period of time\b") {
                            #Close connection 
                            $udpobject.Close() 
                            #Make sure that the host is online and not a false positive that it is open
                            If (Test-Connection -comp $c -count 1 -quiet) {
                                Write-Verbose "Connection Open" 
                                #Build report   
                                #$temp.Server = $c
                                $temp.Port = $p
                                #$temp.TypePort = "UDP"
                                $temp.Open = "Open" 
                            } Else {
                                <#
                                It is possible that the host is not online or that the host is online, 
                                but ICMP is blocked by a firewall and this port is actually open.
                                #>
                                Write-Verbose "Host maybe unavailable" 
                                #Build report   
                                #$temp.Server = $c
                                $temp.Port = $p 
                                #$temp.TypePort = "UDP" 
                                $temp.Open = "Closed or filtered" 
                                #$temp.Notes = "Unable to verify if port is open or if host is unavailable."                                 
                            }                         
                        } ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) {
                            #Close connection 
                            $udpobject.Close() 
                            Write-Verbose "Connection Timeout" 
                            #Build report 
                            #$temp.Server = $c
                            $temp.Port = $p
                            #$temp.TypePort = "UDP"
                            $temp.Open = "Closed or filtered" 
                            #$temp.Notes = "Connection to Port Timed Out"                         
                        } Else {                     
                            $udpobject.close()
                        }
                    }     
                    #Merge temp array with report             
                    $report += $temp
                }                                 
            } 
        }                 
    } 
    End { 
        #Generate Report
        $report
    }
}

function TCPtest()
{

$TCPresults = Test-Port -comp $ip -port 25,137,139,445,4343,9535,9593,9594,9595,9971,9972,12174,16992,16993,16994,33354 -tcp -TCPtimeout 1800
$TCPresults | Out-File $TCPtxt

}

function UDPtest()
{

Clear-Host
Write-Host -ForegroundColor Yellow "`n`nTesting UDP ports used by LANDESK... (This operation can take up to 15sc)"
$UDPresults = Test-Port -comp $ip -port 68,1758,9535,9595,33354,33355,38293 -udp -UDPtimeout 1800
$UDPresults | Out-File $UDPtxt

}

function Fulltest()
{

#Clear-Host
#Write-Host -ForegroundColor Yellow "`n`nTesting TCP and UDP ports used by LANDESK... (This operation can take up to 45sc)"
$TCPresults = Test-Port -comp $ip -port 25,137,139,445,4343,9535,9593,9594,9595,9971,9972,12174,16992,16993,16994,33354 -tcp -TCPtimeout 1800
$TCPresults | Out-File $TCPtxt
$UDPresults = Test-Port -comp $ip -port 68,1758,9535,9595,33354,33355,38293 -udp -UDPtimeout 1800
$UDPresults | Out-File $UDPtxt




$textTCP = "<b>=====>  TCP  <=====</b><br>"
foreach($lineTCP in [System.IO.File]::ReadLines($TCPtxt))
{
      $textTCP += $lineTCP + "<br>"
}
#Write-Host $textTCP



$textUDP = "<b>=====>  UDP  <=====</b><br>"
foreach($lineUDP in [System.IO.File]::ReadLines($UDPtxt))
{
      $textUDP += $lineUDP + "<br>"
}
#Write-Host $textUDP


$EXPLORETCP = "Results are on $Coreserver in Folder: C:\LDPorts\%Hostname%\TCP"
$EXPLOREUCP = "Results are on $Coreserver in Folder: C:\LDPorts\%Hostname%\UCP"



$TCPAnzeige = $Message + $textTCP + $EXPLORETCP

$UDPAnzeige = $Message + $textUDP + $EXPLOREUCP

PostMessageInfo $TCPAnzeige
PostMessageInfo $UDPAnzeige

}

Clear-Host
#$ip = Read-Host "`n`nPlease enter the hostname or the IP address of the client"

#Clear-Host
#Write-Host "`n`nDo you want to check for TCP or UDP ports?"
#Write-Host "`n`n`t1. TCP"
#Write-Host "`n`t2. UDP"
#Write-Host "`n`t3. TCP and UDP"
#$Protocol = Read-Host "`n`nPlease enter your choice number"

Clear-Host

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}
        }

Safe your new customButton


Dont forget to modify roeles and rights to add these new custom button to your five(9)s console role !!!

And now...  
On five(9)s console Home page select an Client on the left (must be online) and click your custom button.

Your result should looks like these:

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