Advertisement
mdriscoll93

Send-UDP-Datagram.ps1

Jul 20th, 2023 (edited)
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.77 KB | Software | 0 0
  1. <#
  2. .SYNOPSIS
  3. Historically, Microsoft hasn't bothered to ship NT with many
  4. useful network debugging tools. It's still best
  5. to use Russinovich's Syinternals utility psping64.exe if possible.
  6. It's a way better tool and isn't limited to 3 parameters. Download:
  7. https://learn.microsoft.com/en-us/sysinternals/downloads/psping.
  8. Likewise, Nir Sofer has written an excellent library of Windows
  9. troubleshooting software available at http://www.nirsoft.net/.
  10. Unfortunately, his software tends to throw Win Defender Alerts.
  11. Feel free to use, change, delete, or distribute this script.
  12.  
  13. .DESCRIPTION
  14. A script that allows user to simulate port-specific
  15. UDP traffic, targeted at IPv4-specific end-stations.
  16. It can be executed interactively or at the shell using params below.
  17.  
  18. .NOTES
  19. File Name      : Send-UDP-Datagram.ps1
  20. Author         : Mark Driscoll
  21. Version        : 1.0
  22. Prerequisite   : PowerShell v5.1
  23. Copyleft 2023  : Acronis Intl. GmbH
  24. Date Created   : 08-08-2023
  25.  
  26. .EXAMPLE
  27. load the script into your environment, then call it by its name and use the params listed
  28. above the function: ---GatwayIPv4 <int> --Port <int> --Message <string>
  29.  
  30.  
  31. .USAGE
  32. Send-UdpDatagram -EndPoint <REMOTE_GW_IP> -Port <UDP_PORT> -Message "test.mymetric:0|c"
  33.  
  34. .Link
  35. [1] https://en.wikipedia.org/wiki/Berkeley_Software_Distribution#cite_note-15
  36. #>
  37.  
  38. param(
  39.     [Parameter()]
  40.     [string]$GatewayIPv4,
  41.     [Parameter()]
  42.     [int]$Port,
  43.     [Parameter()]
  44.     [string]$Message = "test.metric:0|c"
  45. )
  46.  
  47. function Send-UdpDatagram
  48. {
  49.     Param ([string] $EndPoint,
  50.            [int] $Port,
  51.            [string] $Message)
  52.  
  53.     $IP = [System.Net.Dns]::GetHostAddresses($EndPoint)
  54.     $Address = [System.Net.IPAddress]::Parse($IP)
  55.     $EndPoints = New-Object System.Net.IPEndPoint($Address, $Port)
  56.     $Socket = New-Object System.Net.Sockets.UDPClient
  57.     $EncodedText = [Text.Encoding]::ASCII.GetBytes($Message)
  58.     $SendMessage = $Socket.Send($EncodedText, $EncodedText.Length, $EndPoints)
  59.     $Socket.Close()
  60.  
  61.     Write-Host $SendMessage
  62.     Write-Host "Sent message: '$Message' to ${$EndPoint}:$Port"
  63. }
  64.  
  65. if (-not $GatewayIPv4) {
  66.     $GatewayIPv4 = Read-Host -Prompt "Enter the target IP Address"
  67. }
  68.  
  69. if (-not $Port) {
  70.     $Port = Read-Host -Prompt "Enter the UDP Port"
  71. }
  72.  
  73. if (-not $Message) {
  74.     $Message = Read-Host -Prompt "Enter the Message to send (default: test.metric:0|c)"
  75. }
  76.  
  77. if ($GatewayIPv4 -and $Port -and $Message) {
  78.     Write-Host "All parameters are pre-defined. Exiting the loop."
  79.     break
  80. }
  81.  
  82. $endTime = (Get-Date).AddSeconds(600)
  83.  
  84. # Continue looping until finished capturing packets; else, timer terminates after 300s
  85. while ((Get-Date) -lt $endTime)
  86. {
  87.     Send-UdpDatagram -EndPoint $GatewayIPv4 -Port $Port -Message $Message
  88.     Start-Sleep -Seconds 2
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement