Skip to content

Instantly share code, notes, and snippets.

@joshfinley
Last active February 19, 2025 21:19
Show Gist options
  • Save joshfinley/d3aa1e59125f29446906125d441e1db6 to your computer and use it in GitHub Desktop.
Save joshfinley/d3aa1e59125f29446906125d441e1db6 to your computer and use it in GitHub Desktop.
# Define ports to check
$ports = @(22, 53, 80, 443, 445)
# Get IPs from arp -a
$ips = arp -a | ForEach-Object {
if ($_ -match '(\d+\.\d+\.\d+\.\d+)') {
$matches[1]
}
} | Where-Object { $_ -ne "0.0.0.0" -and $_ -ne "255.255.255.255" }
# Function to check open ports
function Check-Port {
param (
[string]$ip,
[int]$port
)
$tcp = New-Object System.Net.Sockets.TcpClient
if ($tcp.ConnectAsync($ip, $port).Wait(100)) {
Write-Output "Port $port is OPEN on $ip"
}
$tcp.Close()
}
# Loop through IPs and ports, only print open ports
foreach ($ip in $ips) {
foreach ($port in $ports) {
Check-Port -ip $ip -port $port
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment