-
Notifications
You must be signed in to change notification settings - Fork 118
/
port-scan-tcp-compat.ps1
44 lines (43 loc) · 1.15 KB
/
port-scan-tcp-compat.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Function port-scan-tcp {
param($hosts,$ports)
if (!$ports) {
Write-Host "usage: port-scan-tcp <host|hosts> <port|ports>"
Write-Host " e.g.: port-scan-tcp 192.168.1.2 445`n"
return
}
$out = ".\scanresults.txt"
foreach($p in [array]$ports) {
foreach($h in [array]$hosts) {
$x = (gc $out -EA SilentlyContinue | select-string "^$h,tcp,$p,")
if ($x) {
gc $out | select-string "^$h,tcp,$p,"
continue
}
$msg = "$h,tcp,$p,"
try {
$t = new-Object system.Net.Sockets.TcpClient
$c = $t.BeginConnect($h,$p,$null,$null)
$w = $c.AsyncWaitHandle.WaitOne(1000,$false)
$r = "Closed"
if ($w) {
$null = $t.EndConnect($c)
$r = "Open"
}
$t.Close();
} catch {
$r = "Error"
}
$msg += $r
Write-Host "$msg"
echo $msg >>$out
}
}
}
# Examples:
#
# port-scan-tcp 10.10.0.1 137
# port-scan-tcp 10.10.0.1 (135,137,445)
# port-scan-tcp (gc .\ips.txt) 137
# port-scan-tcp (gc .\ips.txt) (135,137,445)
# 0..255 | foreach { port-scan-tcp 10.10.0.$_ 137 }
# 0..255 | foreach { port-scan-tcp 10.10.0.$_ (135,137,445) }