mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-08-08 20:54:38 +02:00
Added send-udp.ps1
This commit is contained in:
36
Scripts/send-udp.ps1
Executable file
36
Scripts/send-udp.ps1
Executable file
@ -0,0 +1,36 @@
|
||||
#!/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
|
||||
|
||||
param([string]$IP, [int]$Port, [string]$Message)
|
||||
|
||||
if ($IP -eq "" ) {
|
||||
$IP = read-host "Enter target IP address"
|
||||
}
|
||||
if ($Port -eq 0 ) {
|
||||
$Port = read-host "Enter target port"
|
||||
}
|
||||
if ($Message -eq "" ) {
|
||||
$Message = read-host "Enter message to send"
|
||||
}
|
||||
|
||||
function Send-UdpDatagram
|
||||
{
|
||||
param([string]$EndPoint, [int]$Port, [string]$Message)
|
||||
|
||||
$IP = [System.Net.Dns]::GetHostAddresses($EndPoint)
|
||||
$Address = [System.Net.IPAddress]::Parse($IP)
|
||||
$EndPoints = New-Object System.Net.IPEndPoint($Address, $Port)
|
||||
$Socket = New-Object System.Net.Sockets.UDPClient
|
||||
$EncodedText = [Text.Encoding]::ASCII.GetBytes($Message)
|
||||
$SendMessage = $Socket.Send($EncodedText, $EncodedText.Length, $EndPoints)
|
||||
$Socket.Close()
|
||||
}
|
||||
|
||||
Send-UdpDatagram $IP $Port $Message
|
||||
echo "Done."
|
||||
exit 0
|
Reference in New Issue
Block a user