Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Last active August 14, 2019 12:11
Show Gist options
  • Save FrankSpierings/a1f3a54e197e825b589cffbe5c7c7e5f to your computer and use it in GitHub Desktop.
Save FrankSpierings/a1f3a54e197e825b589cffbe5c7c7e5f to your computer and use it in GitHub Desktop.
Portscanning
#requires -Version 1
function Test-Port
{
Param([string]$ComputerName,$port = 5985,$timeout = 1000)
try
{
$tcpclient = New-Object -TypeName system.Net.Sockets.TcpClient
$iar = $tcpclient.BeginConnect($ComputerName,$port,$null,$null)
$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
if(!$wait)
{
$tcpclient.Close()
return $false
}
else
{
# Close the connection and report the error if there is one
$null = $tcpclient.EndConnect($iar)
$tcpclient.Close()
return $true
}
}
catch
{
$false
}
}
$VerbosePreference = "Continue"
$target = "portquiz.net"
Start-Transcript -Path "$env:temp\portscan-from-$env:computername-2-$target.txt"
1..65535 |% {$port=$_; Write-Verbose "Testing: $port"; if (Test-Port -port $port -ComputerName $target -timeout 400) {Write-Host "Open $port"}}
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment