testPorts - Loop through csv and test for open tcp ports using powershell

Simply put, I got tired of using telnet on a bunch of targets while I tested to see if my firewall team got rules in place. Sometimes I am nested deep into a secure network and cannot get to some of the other utilities out there for download.

Setup servers.csv to have each node on a new line and the format of $server, $tcpPort

The code

[https://github.com/etcSudoers/testPorts]

testPorts.ps1

# Loop through servers.csv and attept to create an tcp connection to each
$csv = Import-Csv servers.csv -Header ("Server","Port")
foreach ($line in $csv) {
  Write-Host "Attempting to connect to " $line.Server " on port " $line.Port
  $tcp = New-Object Net.Sockets.TcpClient
  try
   {
      $tcp.Connect($line.Server,$line.Port)
   } catch {}

   if($tcp.Connected)
   {
       $tcp.Close()
       $msg = "** Connected! **"
   }
   else
   {
      $msg = "** Failed! **"
   }
    Write-Host $msg
}

servers.csv

someserver.example.com, 1433
anotherserver.example.com, 1433
Avatar
Andrew Martin
Lead BI Infrastructure Architect
comments powered by Disqus