Skip to content

Instantly share code, notes, and snippets.

@jmurphyau
Last active July 17, 2019 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmurphyau/fdf6f97011c60fbe63752db3c86c12d4 to your computer and use it in GitHub Desktop.
Save jmurphyau/fdf6f97011c60fbe63752db3c86c12d4 to your computer and use it in GitHub Desktop.
One line powershell to check open ports
powershell -noprofile -command "& { function conn([string]$h,[int]$p) {$c=New-Object System.Net.Sockets.TcpClient; try {$c.connect($h, $p);return [byte]($c.Connected);}catch{return 0}} function hostport([string]$s){$r=($s -match \"^((?:\d{1,3}\.){3}\d{1,3}):(\d{1,6})$\");$m=$matches;return ($r,$m[1],$m[2])} function chk([string]$dest) { $hp=hostport $dest; if ($hp[0]) { write-host (\"Connection to {0}:{1} = {2}\" -f ($hp[1],$hp[2],@(\"Failed\",\"Success\")[(conn $hp[1] $hp[2])])) } else { write-host (\"Invalid: {0}\" -f $dest) } } foreach ($a in $args) {chk $a} }" 127.0.0.1:22 127.0.0.1:80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment