Send and receive data over UDP in PowerShell

I am trying to write a script for testing and application using PowerShell. The test should consist in sending the string to the remote server via UDP, then reading the response from this server and doing something with the result. The only help I need is the middle two (steps "send a line" and then "get an answer") script:

  • Send the string "ABCDEFG" to the server 10.10.10.1 on the UDP port 5000
  • Get a response from the server 10.10.10.1

I am relatively familiar with PowerShell, but this is my first time dealing with sockets, so I am in unfamiliar waters and I cannot understand some examples that I found in the posts.

+6
source share
3 answers

Some time ago, I wrote a simple PowerShell script to send a UDP datagram. See: http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html , which takes you halfway. I never did the second half and wrote the server side of it, though!

<# .SYNOPSIS Sends a UDP datagram to a port .DESCRIPTION This script used system.net.socckets to send a UDP datagram to a particular port. Being UDP, there no way to determine if the UDP datagram actually was received. for this sample, a port was chosen (20000). .NOTES File Name : Send-UDPDatagram Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK http://www.pshscripts.blogspot.com .EXAMPLE #> ### # Start of Script ## # Define port and target IP address # Random here! [int] $Port = 20000 $IP = "10.10.1.100" $Address = [system.net.IPAddress]::Parse($IP) # Create IP Endpoint $End = New-Object System.Net.IPEndPoint $address, $port # Create Socket $Saddrf = [System.Net.Sockets.AddressFamily]::InterNetwork $Stype = [System.Net.Sockets.SocketType]::Dgram $Ptype = [System.Net.Sockets.ProtocolType]::UDP $Sock = New-Object System.Net.Sockets.Socket $saddrf, $stype, $ptype $Sock.TTL = 26 # Connect to socket $sock.Connect($end) # Create encoded buffer $Enc = [System.Text.Encoding]::ASCII $Message = "Jerry Garcia Rocks`n"*10 $Buffer = $Enc.GetBytes($Message) # Send the buffer $Sent = $Sock.Send($Buffer) "{0} characters sent to: {1} " -f $Sent,$IP "Message is:" $Message # End of Script 
+5
source

Ok, so the friend above gave you the client side, and here is a simple server-side code:

$port = 2020
$endpoint = new-object System.Net.IPEndPoint ([IPAddress]::Any,$port)
$udpclient = new-Object System.Net.Sockets.UdpClient $port
$content = $udpclient.Receive([ref]$endpoint)
[Text.Encoding]::ASCII.GetString($content)

  • You can test using IP 127.0.0.1 for your computer on the client side by opening 2 powershell windows (one for the client side and the other for the server side).

  • For more than 1 package, you can use the following code:

$port = 2020
$endpoint = New-Object System.Net.IPEndPoint ([IPAddress]::Any, $port)
Try {
while($true) {
$socket = New-Object System.Net.Sockets.UdpClient $port
$content = $socket.Receive([ref]$endpoint)
$socket.Close()
[Text.Encoding]::ASCII.GetString($content)
}
} Catch {
"$($Error[0])"
}

+1
source

Here is my code:

 $client = new-object net.sockets.udpclient(0) write-host "You are $(((ipconfig) -match 'IPv').split(':')[1].trim()):$($client.client.localendpoint.port)" $peerIP = read-host "Peer IP address" $peerPort = read-host "Peer port" $send = [text.encoding]::ascii.getbytes("heyo") [void] $client.send($send, $send.length, $peerIP, $peerPort) $ipep = new-object net.ipendpoint([net.ipaddress]::any, 0) $receive = $client.receive([ref]$ipep) echo ([text.encoding]::ascii.getstring($receive)) $client.close() 

It performs the following actions:

  • Creates a UDPClient with an automatically assigned port (0).
  • Gets the local IP address, and UDPClient automatically assigns the port and displays it to the user.
  • Retrieves the IP address and port of a peer-to-peer network from a user.
  • Converts the string "heyo" from ASCII encoding to an array of bytes and sends it to the peer. (I believe he will sit there at the equal end until he is β€œaccepted,” even for a few seconds.)
  • Creates an IPEndPoint that receives a UDP packet from any IP address and any port (0).
  • Retrieves any data sent from the peer as a new byte array, with this IPEndPoint as the reference parameter (which now saves the source code of the received packet).
  • Converts the resulting byte array to an ASCII encoded string and prints it.
  • Closes UDPClient. (Be sure to do this, otherwise the resources will be saved (until you restart the PS)!

The beauty of this script is that it is very simple and straightforward, and you can use it either with localhost / 127.0.0.1 (in two separate PowerShell windows), or with an external IP address, which, if it is a local IP address, You already know, because the local IP address is printed for you by the script.

Note that there are SendAsync and ReceiveAsync for UDPClient, but there is no timeout for them. Some people have prepared complex workarounds for this, but you can also just use the PowerShell Start-Job commands and other *-Job and put the receive loop in a separate startup code.

0
source

Source: https://habr.com/ru/post/923904/


All Articles