PowerShell/Scripts/send-udp.ps1

28 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-04-07 11:53:57 +02:00
#!/usr/bin/pwsh
2020-12-29 15:14:21 +01:00
<#
2021-04-07 15:17:49 +02:00
.SYNTAX send-udp.ps1 [<target-IP>] [<target-port>] [<message>]
2021-03-22 20:10:18 +01:00
.DESCRIPTION sends a UDP datagram message to the given IP address and port
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-11-28 09:18:36 +01:00
2021-02-18 20:17:55 +01:00
param($TargetIP = "", [int]$TargetPort = 0, $Message = "")
2021-04-07 15:17:49 +02: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:18:36 +01:00
2020-11-28 09:43:13 +01:00
try {
$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()
2021-02-10 19:25:48 +01:00
write-host -foregroundColor green "Done."
2020-11-28 09:43:13 +01:00
exit 0
2020-12-09 10:30:55 +01:00
} catch {
2021-02-16 10:03:20 +01:00
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}