Showing posts with label system.net.ipendpoint. Show all posts
Showing posts with label system.net.ipendpoint. Show all posts

Friday, 5 December 2008

Send-UDPDatagram.ps1

  1. <#  
  2. .SYNOPSIS 
  3.     Sends a UDP datagram to a port 
  4. .DESCRIPTION 
  5.     This script used system.net.socckets to send a UDP 
  6.     datagram to a particular port. Being UDP, there's 
  7.     no way to determine if the UDP datagram actually 
  8.     was received.  
  9.     for this sample, a port was chosen (20000). 
  10. .NOTES 
  11.     File Name  : Send-UDPDatagram 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 CTP3 
  14. .LINK 
  15.     http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17. #> 
  18.  
  19. ### 
  20. #  Start of Script 
  21. ## 
  22.  
  23. # Define port and target IP address 
  24. # Random here! 
  25. [int] $Port = 20000 
  26. $IP = "10.10.1.100" 
  27. $Address = [system.net.IPAddress]::Parse($IP
  28.  
  29. # Create IP Endpoint 
  30. $End = New-Object System.Net.IPEndPoint $address, $port 
  31.  
  32. # Create Socket 
  33. $Saddrf   = [System.Net.Sockets.AddressFamily]::InterNetwork 
  34. $Stype    = [System.Net.Sockets.SocketType]::Dgram 
  35. $Ptype    = [System.Net.Sockets.ProtocolType]::UDP 
  36. $Sock     = New-Object System.Net.Sockets.Socket $saddrf, $stype, $ptype 
  37. $Sock.TTL = 26 
  38.  
  39. # Connect to socket 
  40. $sock.Connect($end
  41.  
  42. # Create encoded buffer 
  43. $Enc     = [System.Text.Encoding]::ASCII 
  44. $Message = "Jerry Garcia Rocks`n"*10 
  45. $Buffer  = $Enc.GetBytes($Message
  46.  
  47. # Send the buffer 
  48. $Sent   = $Sock.Send($Buffer
  49. "{0} characters sent to: {1} " -f $Sent,$IP 
  50. "Message is:" 
  51. $Message 
  52. # End of Script 

 

If you run this script and are using WireShark to capture the network traffic, you might see something like this:

image

From this trace, you can see the UDP Datagram that was sent. And since the port does not exist on the target machine (10.10.1.100), that machine returns a Destination Port Unreachable as expected.