Saturday, July 12, 2008

Obtain external IP

Recently I had to obtain the external IP of a number of PC - ie. the address the PCs appear to use on the internet (in my case, the external IP address of their internet gateway/router).

Here is the small function I ended up using:

## 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
}

Invoke the function in the following way:

$myip = Get-ExternalIP
$myip.externalip

5 comments:

Sean McCown said...

I love your blog. You've done a lot of very useful work and I'm copying most of your scripts.

However, what I don't see is a way to save these outputs to SQL. I want to say take the Win32_volume query and pipe it to a sql table. Is that possible?
Thanks, and keep up the scripting. It's great.

Sean McCown
KO@KenpoSecrets.com

Jakob Bindslet said...

Thanks for the kind words Sean.

You are absolutely right, my blog is rather thin on traditional SELECT or INSERT type functionality. I'll try to remedy this in the future.

Please let me (and the rest of the community) know of all the cool stuff you will use PowerShell to accomplish ! :-)

Thomas said...

Nice trick. I've copied this, and some other tricks, to a blog posting at http://tfl09.blogspot.com/2008/07/finding-your-external-ip-address-with.html.

THANKS!!!

Interesting facts said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
html5 video server| html5 server

holy joly said...

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