Skip to content

Instantly share code, notes, and snippets.

@itpropro
Created July 9, 2019 11:45
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 itpropro/5c6c5a430610a320fc37ccf13147b288 to your computer and use it in GitHub Desktop.
Save itpropro/5c6c5a430610a320fc37ccf13147b288 to your computer and use it in GitHub Desktop.
$target = '8.8.8.8'
$udpPort = 53
$packetSize = 64
### Using UdpClient
[byte[]]$packet = [byte[]]::new($packetSize)
$udpClient = [System.Net.Sockets.UdpClient]::new()
$udpClient.Client.ReceiveTimeout = 1000
$ipEndPoint = [System.Net.IPEndPoint]::new([System.Net.IPAddress]::Any, 0)
try {
$udpClient.Connect($target, $port)
$null = $udpClient.Send($packet, $packetSize)
$null = $udpClient.Receive([ref]$ipEndPoint)
Write-Host "Port $port on $target replies"
}
catch {
Write-Host $($_.Exception.Message)
Write-Host "Port $port on $target does not reply"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment