Skip to content

Instantly share code, notes, and snippets.

@abelmferreira
Created October 16, 2017 13:57
Show Gist options
  • Save abelmferreira/8e24ac2987039e2098f20705f3337518 to your computer and use it in GitHub Desktop.
Save abelmferreira/8e24ac2987039e2098f20705f3337518 to your computer and use it in GitHub Desktop.
Powershell script para scan de portas abertas em uma lista de portas em uma lista de ips
$portrange = 1226..1260
$net = "10.0.0"
$range = 2..2
$timeout_ms = 5
foreach ($r in $range)
{
Get-Date
$ip = "{0}.{1}" -F $net,$r
if (Test-Connection -BufferSize 32 -Count 1 -Quiet -ComputerName $ip)
{
Write-Host "IP $ip is alive... checking ports..."
foreach ($port in $portrange)
{
$ErrorActionPreference = 'SilentlyContinue'
$socket = new-object System.Net.Sockets.TcpClient
$connect = $socket.BeginConnect($ip, $port, $null, $null)
$tryconnect = Measure-Command { $success = $connect.AsyncWaitHandle.WaitOne($timeout_ms, $true) } | % totalmilliseconds
$tryconnect | Out-Null
if ($socket.Connected) {
"$ip is listening on port $port (Response Time: $tryconnect ms)"
$socket.Close()
$socket.Dispose()
$socket = $null
} else {
Write-Host "port $port not open on $ip" -foregroundcolor "red"
}
$ErrorActionPreference = 'Continue'
}
Get-Date
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment