Created
July 9, 2019 11:45
-
-
Save itpropro/5c6c5a430610a320fc37ccf13147b288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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