# Echo-udp # # Start a UDP-Listener ona given Ip:Port and wait für packets to echo bach # # Links: # http://www.msxfaq.de/code/powershell/psudp.htm # https://www.msxfaq.de/tools/prtg/prtgqosudp.htm # http://pshscripts.blogspot.de/2008/12/send-udpdatagramps1.html # https://GitHub.com/PaesslerAG/QoSReflect/blob/master/qosreflect.py [CMDLetBinding()] param ( [int]$listenudpport = 50000, # port where to listen fpor incoming packets [int]$remoteport=50000, # optional. specify an alternate target port for Echo. PRTG requires that [string]$remoteip= "" # specify IP Address to limit rely to a certain addess. Nut it can still flood that IP ) set-psdebug -strict $error.clear() write-host "echo-udp: Start" try { $udpClient = New-Object system.Net.Sockets.Udpclient($listenudpport) $RemoteIpEndPoint = New-Object System.Net.IPEndPoint([system.net.IPAddress]::Parse("0.0.0.0") , 0); Write-host "echo-udp:Wait für Data on Port: $listenudpport" [int]$count=0 while ($true) { $data= $udpclient.receive([ref]$RemoteIpEndPoint) # wait für data arriving from any ip. Alternativ kann man eine Liste der erlaubten IPs angeben if (($remoteip -ne "") -and ($remoteip -ne $RemoteIpEndPoint.address.IPAddressToString)) { write-host "Drop Packet from $($RemoteIpEndPoint.address.IPAddressToString)" } else { $count++ if ($count -ge 10) { $count = 0 write-host "." -nonewline } write-verbose ("echo-udp:Received packet from IP " + $RemoteIpEndPoint.address + ":" + $RemoteIpEndPoint.Port) #write-debug ("echo-udp:Content" + ([string]::join("",([System.Text.Encoding]::ASCII.GetChars($data))))) if ($remoteport) { $sentbytes = $udpClient.Send($data, $data.length, $RemoteIpEndPoint.address, $remoteport) } else { $sentbytes = $udpClient.Send($data, $data.length, $RemoteIpEndPoint.address, $RemoteIpEndPoint.port) } if ($sentbytes -ne $data.length) { write-host "echo-udp:Send Bytes Mismatch" } } } } catch { write-host "echo-udp:Error occured $error" } finally { write-host "echo-udp:Closing" write-host "echo-udp: End" $udpclient.close() }