Skip to content

Instantly share code, notes, and snippets.

@joeypiccola
Created June 10, 2019 17:39
Show Gist options
  • Save joeypiccola/5f0816174c1a91dfc1d92ca308ab23d2 to your computer and use it in GitHub Desktop.
Save joeypiccola/5f0816174c1a91dfc1d92ca308ab23d2 to your computer and use it in GitHub Desktop.
test net ports from mr bush
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True)]
[String]
$target_node,
[Parameter(Mandatory = $True)]
[Int]
$target_port
)
# Make all errors terminating
$ErrorActionPreference = 'Stop'
# If the value for $target_node contains letters, assume it's a hostname and try a DNS lookup before proceeding,
# but don't output the lookup results. If the lookup fails, the script will exit at this point.
if ($target_node -match '[a-zA-Z]') { Resolve-DnsName $target_node > $null }
# With an IP or resolvable hostname supplied, run the port test. If no errors are thrown, output a success message.
try {
(New-Object Net.Sockets.TcpClient).Connect($target_node, $target_port)
Write-Output "Port $target_port on $target_node is OPEN"
}
# If an error is thrown during port test, write a failure message.
catch {
Write-Error "Port $target_port on $target_node is CLOSED"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment