SCRIPT: Check your WAN IP and get Notified on change

One of the first powershell scripts I wanted to share is a script that checks your WAN ip and notifies you by e-mail when it changes.

Background:

I’ve used to sign up with a dynamic DNS provider but my router stopped updating my ip address to that DynDNS provider. After a while I received an e-mail that my host was being deleted from the database if I did not take action. A few e-mail later I decided to say goodbye to that DynDNS provider because my IP-Address does not change that much at all (I still have the same address as a year ago). So I wrote a script that checks the IP address of your FQDN and uses the automation page of  WhatisMyIP.com  to read you current WAN ip address and sends you an e-mail when it has changed.

Code Break down:

This is where you put your router’s FQDN

$TargetDomain = "Your Host FQDN" #Enter your FQDN that is linked to your WAN ip address

This function takes care of sending the e-mail

Function SendMail {
$EmailFrom = "from@yourdomain.com" #Enter the e-mail address it should send the message from.
$EmailTo = "to@yourdomain.com" #Enter the e-mail address it should send the message to.
$Subject = "External IP Address Has Changed ($Current_WAN_IP)" #Enter your subject the e-mail message should contain.
$Body = "Please Update your DNS records to $Current_WAN_IP" #Enter the message that the e-mail should contain.
$SMTPServer = "SMTP SERVER FQDN" #Enter the FQDN of your SMTP server eg. smtp.gmail.com
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) #Enter your SMTP Port eg. 587
$SMTPClient.EnableSsl = $true # Change to $false if you don't want to use SSL
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("your smtp username", "your smtpserver password") #Comment line with '#' if you don't need authentication
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}

Here is where the magic happens, this code checks your WAN IP and executes the correct action

# Do not change anything below this line.
$DNS_WAN_IP = [System.Net.Dns]::GetHostAddresses("$TargetDomain") | select-object IPAddressToString -expandproperty IPAddressToString
Write-host "Resolved IP Address of $TargetDomain = $DNS_WAN_IP"
$Current_WAN_IP = (New-Object System.Net.WebClient).DownloadString("http://automation.whatismyip.com/n09230945.asp")
Write-host "Current WAN IP Address = $Current_WAN_IP"
if ($Current_WAN_IP -ne $DNS_WAN_IP) {
 if ((@([Byte[]][char[]]$Current_WAN_IP -eq 46).Count)-ne 3) {write-host "No Valid IP Address Retrieved";Exit}
 Write-Host "IP Address Has Changed, Sending e-mail"
 SendMail
 exit
 }
 Else {write-host "No IP Address Change Detected";Exit}

This script is tested with Powershell V2 but should work in V1 as well. I’ll Hope you like the script full version can be downloaded here: CheckWANIP (1265)

8 Comments to “SCRIPT: Check your WAN IP and get Notified on change”

  1. Exotic Hadron 18 December 2012 at 14:54 #

    Hi,
    Thank you for this nice script. I guess, however, it has a defect in line 2 of the checking snippet.
    What does $TargetDomain variable resolve to? It’s specified nowhere in this snippet. I believe
    [String]$TargetDomain = [System.DirectoryServices.ActiveDirectory.Domain]::getCurrentDomain()
    Correct?
    Thank you,
    EH

  2. Exotic Hadron 18 December 2012 at 15:04 #

    For sure, I meant the attached ps1 file. It’s missing the fist line:
    $TargetDomain = “Your Host FQDN” #Enter your FQDN that is linked to your WAN ip address

    Thank you.

  3. Exotic Hadron 18 December 2012 at 15:06 #

    Heck, Leon,
    Forget these posts. I must have been blind to not see the obvious things and overlooked the first line.

    Thank you, Sir.

  4. CoffeeQuaffer 2 January 2014 at 20:50 #

    Great script, perfect for my needs. Thanks for working this out and posting it!

    Perry

  5. Cats 8 January 2015 at 22:44 #

    Great script!!

    To bad the script is not working anymore since the URL:
    “http://automation.whatismyip.com/n09230945.asp” is no longer active.

    Do you have a solution for this?

    Info from the whatismyip website:

    We welcome programmers who incorporate our site into their work, however as of late we have experienced some abuse of our rules. We thought some people may not be familiar with our rules concerning automated files so we thought it best to post a page for easy reference.

    Thank you,

    Cats

  6. Cats 9 January 2015 at 00:23 #

    ###########################################################################
    #
    # NAME: CheckWANIP
    #
    # AUTHOR: Leon van Efferen
    # Changed by: Robert Catsburg
    #
    # E-MAIL: leon@efferen.nl
    #
    # COMMENT: Check the WAN IP Address based on What’s My IP Automation page
    # and e-mail a message when it has changed.
    #
    # COMMENT 2.0 : Check the WAN IP Address based on checkip.dyndns.com page
    # and e-mail a message when it has changed.
    # (the MY IP Automation page is no longer working)
    #
    # VERSION HISTORY:
    # 1.0 4-1-2012 – Initial release
    # 1.1 5-1-2012 – Changed IP change detection to Target Domain.
    # 2.0 8-1-2015 – Changed the method to retrieve the external IP address
    #
    ###########################################################################

    #Set Target Domain
    $TargetDomain = “Your Host FQDN” #Enter your FQDN that is linked to your WAN ip address

    Function SendMail {
    $EmailFrom = “from@yourdomain.com” #Enter the e-mail address it should send the message from.
    $EmailTo = “to@yourdomain.com” #Enter the e-mail address it should send the message to.
    $Subject = “External IP Address Has Changed” #Enter your subject the e-mail message should contain.
    $Body = “Please Update your DNS records to $Current_WAN_IP” #Enter the message that the e-mail should contain.
    $SMTPServer = “SMTP SERVER FQDN” #Enter the FQDN of your SMTP server eg. smtp.gmail.com
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) #Enter your SMTP Port eg. 587
    $SMTPClient.EnableSsl = $true # change to $false if you don’t want to use SSL
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“your smtp username”, “your smtpserver password”) #Comment line with ‘#’ if you don’t need authentication
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    }

    # Do not change anything below this line.

    $url = “http://checkip.dyndns.com” # Defining the website url in a variable
    $webclient = New-Object System.Net.WebClient # Creating a new .Net Object names a System.Net.Webclient
    $Ip = $webclient.DownloadString($url) # In this new webdownlader object we are telling $webclient to download the url $url
    $Ip2 = $Ip.ToString() # Text manuplation to get the ipadress form downloaded URL
    $ip3 = $Ip2.Split(” “)
    $ip4 = $ip3[5]
    $ip5 = $ip4.replace(“”,””)
    $Current_WAN_IP = $ip5.replace(“”,””)
    $DNS_WAN_IP = [System.Net.Dns]::GetHostAddresses(“$TargetDomain”) | select-object IPAddressToString -expandproperty IPAddressToString

    Write-host “Current WAN IP Address = $Current_WAN_IP”
    Write-host “Resolved IP Address of $TargetDomain = $DNS_WAN_IP”

    if ($Current_WAN_IP -notmatch $DNS_WAN_IP) {
    if ((@([Byte[]][char[]]$Current_WAN_IP -eq 46).Count)-ne 3) {write-host “No Valid IP Address Retrieved”;Exit}
    Write-Host “IP Address Has Changed, Sending e-mail”
    SendMail
    exit
    }
    Else {write-host “No IP Address Change Detected”;Exit}

  7. heckond 1 October 2015 at 23:18 #

    Very good script… PFB an option that works with this script:
    http://icanhazip.com/


Leave a Reply