Monthly Archives: March 2013

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.

2 Comments

Filed under Powershell