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

Wednesday, 6 October 2010

Get-LoopBack.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script checks whether a parameter is a Loopback Address  
  4. .DESCRIPTION 
  5.     This script checks to see if the passsed string is an IPV4
  6.     or an IPv6 loopback address and if so, displays details.
  7. .NOTES 
  8.     File Name  : Get-LoopBack.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com  
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/system.net.ipaddress.isloopback.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-LoopBack.ps1 
  18.     Your input address: \127.0.0.1\ is an IPv4 loopback address whose internal format is: 127.0.0.1. 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-LoopBack.ps1 ::1 
  21.     Your input address: \::1\ is an IPv6 loopback address whose internal format is: ::1. 
  22. .EXAMPLE 
  23.     PSH [C:\foo]: .\Get-LoopBack.ps1 131.107.2.200 
  24.     Your input address: \131.107.2.200\ is not a loopback address. 
  25. .PARAM 
  26.     $IPAddress - Address to look up to see if it's Loopback 
  27. #> 
  28. param ( 
  29. [String] $IpAddress = "127.0.0.1" 
  30. )  
  31. # Setup Default answer! 
  32. $loopBack=" is not a loopback address."; 
  33.  
  34. # Perform syntax check by parsing the address string entered by the user. 
  35. $Address = [System.Net.IPAddress]::Parse($IpAddress); 
  36.  
  37. # Perform semantic check by verifying that the address is a valid IPv4  
  38. # or IPv6 loopback address.  
  39. if([System.Net.IPAddress]::IsLoopback($Address) -and ($address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) ) { 
  40.         $loopBack =  " is an IPv6 loopback address " + 
  41.                     "whose internal format is: " + $Address.ToString() + "."; 
  42. }            

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.

Thursday, 4 December 2008

Get-IPAddress2.ps1

# Get-IPAddress2.ps1
# Parses an address, returns a System.NET.IPAddress
if OK
# Uses tryparse (and therefore works with PSH v1)
# Thomas Lee
- tfl@psp.co.uk

$strings
= "10.1.1.100", "10.1", "fooey"

foreach( $string in $strings){
"Testing: `"{0}`"" -f $string
$address
= [System.Net.IPAddress]::tryparse($string,[ref] $ip1)
if ($address ){
"`"{0}`" `tis a valid IP address" -f [System.Net.IPAddress]::parse($string)
}
else {
"`"{0}`" `tis NOT a valid IP address" -f $string
}

}
This script produces the following output:

PS C:\foo> .\get-ipaddress2.ps1
Testing:
"10.1.1.100"
"10.1.1.100" is a valid IP address
Testing:
"10.1"
"10.0.0.1" is a valid IP address
Testing:
"fooey"
"fooey" is NOT a valid IP address