Check port connection from one remote machine to another

This topic contains 1 reply, has 2 voices, and was last updated by  postanote 3 weeks, 4 days ago.

  • Author
    Posts
  • #92255

    Chris Cass
    Participant

    Having a massive brain fart here... I'm attempting to build a script that my customers can use to run a quick health check on a program of ours. The program requires the server that our program is on to be able to communicate with other servers over specific ports. One of the things I'd like to do with this is to check all the connections from our server to the other servers and display if the connection was successful or not. I found this script, which so far works like a charm:

    $hostname = 'HOSTNAME'
    $port = '104'
    
    function Test-Port($hostname, $port)
    {
        # This works no matter in which form we get $host - hostname or ip address
        try {
            $ip = [System.Net.Dns]::GetHostAddresses($hostname) | 
                select-object IPAddressToString -expandproperty  IPAddressToString
            if($ip.GetType().Name -eq "Object[]")
            {
                #If we have several ip's for that address, let's take first one
                $ip = $ip[0]
            }
        } catch {
            Write-Host "Possibly $hostname is wrong hostname or IP"
            return
        }
        $t = New-Object Net.Sockets.TcpClient
        # We use Try\Catch to remove exception info from console if we can't connect
        try
        {
            $t.Connect($ip,$port)
        } catch {}
    
        if($t.Connected)
        {
            $t.Close()
            $msg = "Port $port is operational"
        }
        else
        {
            $msg = "Port $port on $ip is closed, "
            $msg += "You may need to contact your IT team to open it. "                                 
        }
        Write-Host $msg
    }
    
    Test-Port $hostname $port
    

    The only thing I'm stuck on is trying to get the response from one remote machine to another. As it stands, I'm getting the results from the local workstation to the final destination. This doesn't tell me if the connection is good between the 2 servers.

  • #92263

    postanote
    Participant

    You do not say which version of PoSH you are running, but...here is a different approach using all PoSH vs calling .NET
    This is Q&D, but see that this does for your efforts and of course tweak in to your code...

        # Validate connection state of hosts in the environment.
    
        $AdComputers = (Get-ADComputer -Filter *).Name 
    
        ForEach($TargetHost in $AdComputers)
        {
            # Test from localhost to remote AD hosts
            Test-Connection -ComputerName $TargetHost -Count 1
    
            # Use current host valiadate remote hosts
            Invoke-Command -ComputerName $TargetHost -ScriptBlock {
                ForEach ($TargetHost in $Using:AdComputers)        
                {Test-Connection -ComputerName $TargetHost -Count 1}
            }
        }
    
        
        Source    Destination IPV4Address  IPV6Address  Bytes    Time(ms) 
        ------    ----------- -----------  -----------  -----    -------- 
        DC01      EX01        192.168...                32       0
        ...
        EX01      DC01        192.168...                32       0        
        ...       
            
       
        # If you want the port inclusion, then something like this.
    
        $AdComputers = (Get-ADComputer -Filter *).Name 
    
        ForEach($TargetHost in $AdComputers)
        {
            # Test from localhost to remote AD hosts
            "Local processing from source $env:COMPUTERNAME"
            Test-NetConnection -ComputerName $TargetHost -Port 445 | Format-Table -AutoSize
    
            # Use current host valiadate remote hosts
            Invoke-Command -ComputerName $TargetHost -ScriptBlock {
                ForEach ($TargetHost in $Using:AdComputers)        
                {
                    "Remote processing from source $env:COMPUTERNAME"
                    Test-NetConnection -ComputerName $TargetHost -Port 445 | Format-Table -AutoSize        
                }
            }
        }
    
    

You must be logged in to reply to this topic.