Dynamic internet IP addresses and powershell

My lovely ISP won’t offer static IP addresses, and seems intent on changing my home IP as often as possible. This is a bit of a pain, but rather than sign-up to dyn-dns or similar, I wrote a script that I can run on a schedule at home, and then dump a file to dropbox. That way, wherever I am I can find my home IP (well, as recently as the scheduled task ran).

Param([string]$ipfile="dropbox\ipaddress.txt")
$path = "http://whatismyipaddress.com"

#open page
$ie = New-Object -ComObject InternetExplorer.Application
	$ie.Navigate("$path")
	While ($ie.Busy) { Start-Sleep -Milliseconds 200 }
	#Read contents
	$ie.Document.body.innerhtml | Out-File "$env:userprofile\my documents\http.txt"

    $pagecontent = (((get-content "$env:userprofile\my documents\http.txt" `
				| where {$_ -like "*/ip/*"}).Split("="))[$pagecontent.count-3])
    $pagecontent2 = $pagecontent.split(" ")
    $ipadd = $pagecontent2[$pagecontent2.count-2].TrimEnd('"')
    
    
    $ipadd | out-file $ipfile 

If I schedule it to check every four hours or so, I can be fairly confident of always knowing my home IP address.

Advertisements

2 Comments

Filed under Powershell

2 responses to “Dynamic internet IP addresses and powershell

  1. No Name

    Excellent Job! Your script make my day! Thank you.
    According to my test, it seems that the 12th line needs be changed to
    where {$_ -like “*/ip/*”}).Split(“=”))[$pagecontent.count-4])

  2. Glad it helped. To be honest, I was playing around with it on another PC and I think it depends on what OS and browser version you are using, and more specifically, what .NET framework perhaps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s