Tuesday, July 15, 2008

Finding Your External IP address (with PowerShell)

For many of us, the IP address of our PC is not a publicly routable one.  If I run IPConfig on my workstation, I get something that looks like this:

Ethernet adapter Lan1BuiltIn:

   Connection-specific DNS Suffix  . : foobar.net
   Link-local IPv6 Address . . . . . : fe80::3955:f42b:2d1e:1423%10
   IPv4 Address. . . . . . . . . . . : 10.234.12.123
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.234.12.100

But if you were to try to ping me, you’d never get to my system, since I’m behind a firewall. My IP address on the Internet, the publicly routable IP address, is quite different.

There are a lot of ways you can find out what your IP address is – there are a bunch of sites you can use:

No doubt there are a bunch more!

Recently, I’ve seen two other sites used, via PowerShell, to get your IP address. The first is http://checkip.dyndns.org. The nice thing about this site is that it returns very little data, so you can do some ‘screen scraping’ to extract your external IP address. With this site, as shown on Per Ostergaard’s blog, you could do this as follows:

$wc=New-Object net.webclient $wc.downloadstring("http://checkip.dyndns.com") -replace "[^\d\.]"

This is pretty cool – and in discovering this, I discovered the –replace operator that I did not know about before.

The second is also interesting. The source is here.  The script looks like this:

 

## Function to retrieve external IP address. ## the external address is retrieved from the ## title header of the webpage "www.myip.dk" function Get-ExternalIP { $source = "http://www.myip.dk" $client = new-object System.Net.WebClient $webpage = $client.downloadString($source) $lines = $webpage.split("`n") foreach ($line in $lines) { if ($line.contains("</title>")) { $ip = $line.replace(" <title>Your IP address is: ", "").replace("</title>","") } } $obj = New-Object Object $obj | Add-Member Noteproperty externalIP -value $ip $obj }

What this script does is to first call www.myip.dk and return the page. This site is different form the earlier ones in that it puts the IP address into the title bar. The script then parses the returned page, gets the title directive, and pulls the IP address out.

Technorati Tags: ,,

8 comments:

Master Shake said...

I've been using IP Finding quick and easy

Pedro César said...

Im using that function to set my external IP on the Microsoft Loopback Adapter:

Get-WmiObject -class win32_networkadapterconfiguration | where-object -filterscript { $_.ServiceName -eq "msloop" } | foreach-object -process { $_.EnableStatic($ip,"255.255.0.0")}

Im using the $ip on method EnableStatic, and got the error 70 (Invalid IP Address). If I replace the $ip for "182.221.21.57" it run. =/ what must i do?

EIk7LRUZvMEV2FKAEUFT8TgKc_7xlw-- said...

But how do you find the actual internal IP (LAN) address? When you run an 'ipconfig' sometimes there are addresses for other network adapters, vpn connections, etc.

ps1cscpp said...

This is a lot more short:

[void]($webpage -split "`n" | ? {$_ -match "\d*\.\d*\.\d*\.\d*"}); $Matches[0]

Odis Carnovale said...



IP addresses are viewed as a reduced amount of secure compared to dynamic IP addresses,being with the intention of they are simpler to track pro data purposes.On the other furnish,simply following online safety rules and guidelines can help lessen one the makings problems and keep your notebook safe in any case of could you repeat that? Kind of IP take up you decide to aid.For more this information view my friend website http://www.ipaddresshub.com/

Odis Carnovale said...


IP addresses are viewed as a reduced amount of secure compared to dynamic IP addresses,being with the intention of they are simpler to track pro data purposes.On the other furnish,simply following online safety rules and guidelines can help lessen one the makings problems and keep your notebook safe in any case of could you repeat that? Kind of IP take up you decide to aid.For more this information view my friend website what is my ip

holy joly said...

Nice information. I had checked my External ip address using this site
IP-Details.com

wwwillie said...

Thanks Thomas your site gave me what I needed to replace ifconfig.me's output.
I found from getting content on that page there was a subsite that had just the IPv4 address that I wanted. After removing the quotation marks I am back to one line.

((Invoke-WebRequest ipv4.myip.dk/api/info/IPv4Address).Content).replace('"',"")

Here is the HTML that gave it away:
function LoadIPv4Address( jQuery )
{
try {
$('#loadingipv4address').html("");
$('#loadingipv4address').show();

var ipv4address = $.ajax(
{
url: "http://ipv4.myip.dk/api/info/IPv4Address",
datatype: "json",
timeout: 15000
});

ipv4address.done(function (response) {
$('#ipv4address').html("<p>" + response + "</p>");
$('#loadingipv4address').hide();
});

ipv4address.fail(function (jqxhr, textstatus, errorthrown) {
$('#ipv4address').html("<p> Could not determine any IPv4 address.</p>
$('#loadingipv4address').hide();
});
}
catch (e) {
$('#loadingipv4address').hide();
}
}