PowerShell/Scripts/send-udp.ps1

35 lines
1.0 KiB
PowerShell
Raw Normal View History

2020-11-28 09:18:36 +01:00
#!/snap/bin/powershell
# Syntax: ./send-udp.ps1 [<IP>] [<port>] [<message>]
# Description: sends a UDP datagram message to the given IP address and port
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
2020-11-28 09:43:13 +01:00
param([string]$TargetIP, [int]$TargetPort, [string]$Message)
2020-11-28 09:18:36 +01:00
2020-11-28 09:43:13 +01:00
try {
2020-12-25 11:30:43 +01:00
if ($TargetIP -eq "" ) {
$TargetIP = read-host "Enter target IP address"
}
if ($TargetPort -eq 0 ) {
$TargetPort = read-host "Enter target port"
}
if ($Message -eq "" ) {
$Message = read-host "Enter message to send"
}
2020-11-28 09:43:13 +01:00
$IP = [System.Net.Dns]::GetHostAddresses($TargetIP)
$Address = [System.Net.IPAddress]::Parse($IP)
$EndPoints = New-Object System.Net.IPEndPoint($Address, $TargetPort)
$Socket = New-Object System.Net.Sockets.UDPClient
$EncodedText = [Text.Encoding]::ASCII.GetBytes($Message)
$SendMessage = $Socket.Send($EncodedText, $EncodedText.Length, $EndPoints)
$Socket.Close()
2020-12-25 11:30:43 +01:00
write-output "Done."
2020-11-28 09:43:13 +01:00
exit 0
2020-12-09 10:30:55 +01:00
} catch {
2020-12-25 11:30:43 +01:00
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}