- <#
- .SYNOPSIS
- This script checks whether a parameter is a Loopback Address
- .DESCRIPTION
- This script checks to see if the passsed string is an IPV4
- or an IPv6 loopback address and if so, displays details.
- .NOTES
- File Name : Get-LoopBack.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.net.ipaddress.isloopback.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-LoopBack.ps1
- Your input address: \127.0.0.1\ is an IPv4 loopback address whose internal format is: 127.0.0.1.
- .EXAMPLE
- PSH [C:\foo]: .\Get-LoopBack.ps1 ::1
- Your input address: \::1\ is an IPv6 loopback address whose internal format is: ::1.
- .EXAMPLE
- PSH [C:\foo]: .\Get-LoopBack.ps1 131.107.2.200
- Your input address: \131.107.2.200\ is not a loopback address.
- .PARAM
- $IPAddress - Address to look up to see if it's Loopback
- #>
- param (
- [String] $IpAddress = "127.0.0.1"
- )
- # Setup Default answer!
- $loopBack=" is not a loopback address.";
- # Perform syntax check by parsing the address string entered by the user.
- $Address = [System.Net.IPAddress]::Parse($IpAddress);
- # Perform semantic check by verifying that the address is a valid IPv4
- # or IPv6 loopback address.
- if([System.Net.IPAddress]::IsLoopback($Address) -and ($address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) ) {
- $loopBack = " is an IPv6 loopback address " +
- "whose internal format is: " + $Address.ToString() + ".";
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
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
Labels:
powershell,
PowerShell scripts,
system.net.ipaddress
Friday, 5 December 2008
Send-UDPDatagram.ps1
- <#
- .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's
- 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
If you run this script and are using WireShark to capture the network traffic, you might see something like this:
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
}
}
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
Labels:
powershell,
PowerShell scripts,
system.net.ipaddress
Subscribe to:
Posts (Atom)