PowerShell/docs/send-udp.md

106 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *send-udp.ps1* Script
===========================
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
This PowerShell script sends a UDP datagram message to an IP address and port.
2021-10-17 14:33:27 +02:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-10-17 14:33:27 +02:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/send-udp.ps1 [[-TargetIP] <String>] [[-TargetPort] <Int32>] [[-Message] <String>] [<CommonParameters>]
2023-05-26 12:20:18 +02:00
-TargetIP <String>
Specifies the target IP address
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-TargetPort <Int32>
Specifies the target port number
Required? false
Position? 2
Default value 0
Accept pipeline input? false
Accept wildcard characters? false
2021-10-17 14:33:27 +02:00
2023-05-26 12:20:18 +02:00
-Message <String>
Specifies the message text to send
Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
2021-10-17 14:33:27 +02:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2023-05-26 12:20:18 +02:00
```powershell
PS> ./send-udp 192.168.100.100 8080 "TEST"
2024-11-08 12:35:11 +01:00
✅ Done.
2023-05-26 12:20:18 +02:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2023-05-26 12:20:18 +02:00
Author: Markus Fleschutz | License: CC0
2023-07-29 10:04:38 +02:00
Related Links
-------------
2023-05-26 12:20:18 +02:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Sends a UDP datagram message to an IP address and port
.DESCRIPTION
This PowerShell script sends a UDP datagram message to an IP address and port.
.PARAMETER TargetIP
Specifies the target IP address
.PARAMETER TargetPort
Specifies the target port number
.PARAMETER Message
Specifies the message text to send
.EXAMPLE
PS> ./send-udp 192.168.100.100 8080 "TEST"
2024-11-08 12:35:11 +01:00
✅ Done.
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-05-26 12:20:18 +02:00
param([string]$TargetIP = "", [int]$TargetPort = 0, [string]$Message = "")
2022-11-17 20:02:26 +01:00
try {
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" }
$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()
2024-11-08 12:35:11 +01:00
"✅ Done."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:52:00)*