server script that sends an email every time the global ip changes

I'm setting up a web server that won't have a static global IP. It will be at my parents house and I won't be able to SSH into it when the global IP changes. The global IP will change every time router is restarted which happens like once a month.

I need a script that sends me an email every time the global IP changes. So I can update the DNS so my website will work again.

I don't know anything about scripts in Ubuntu, but I know the basics in Java and PHP.

Computer: Ubuntu 13.10 server, iMac G4 Globe.


Have you considered using dynamic DNS update script/tool?

e.g. ddclient available as Debian/Ubuntu package.

It can update your DNS zone OR update your dynamic DNS record (so you can ssh in) and execute custom script to send you email.


Based on the code from this answer I have modified and added a few more options for debugging.

I had to set up the email server and use crontab -e to get this to email me my current ip address. You will also need to make the script file executable and make sure your user has read and write permission to the folder you put the scripts in.

#!/bin/bash
NOWIPADDR="/home/scripts/nowipaddr"
GETIPADDR="dig +short myip.opendns.com @resolver1.opendns.com"
LOG="/home/scripts/ip.log"
timestamp=$( date +%T )
curDate=$( date +"%m-%d-%y" )

if [ -f $NOWIPADDR ]; then
  if [[ `$GETIPADDR` = $(< $NOWIPADDR) ]]; then
    echo $curDate $timestamp " IP address check: " $(< $NOWIPADDR) >> $LOG
  else
    $GETIPADDR > $NOWIPADDR
    mail -s "Server IP" [email protected] < $NOWIPADDR
  fi
else
  $GETIPADDR > $NOWIPADDR
  mail -s "Server IP" [email protected] < $NOWIPADDR
fi

You can write a simple script to run from the cron and daily at particular time.

Take the existing ip in a file and then run your if loop to check the new ip with the existing ip and if both the ip's remain same, it can skip sending mail. Otherwise, if there is a change in IP it should send mail with the new ip.

you can run the script hourly, daily, weekly.

NOWIPADDR="nowipaddr"
GETIPADDR="ifconfig.me"


    if [ -f $NOWIPADDR ]
    then
       if [ `cat $NOWIPADDR` = `curl $GETIPADDR` ]
       then
           echo "no change in IP."
       else
           curl $GETIPADDR > $NOWIPADDR
           mail -s "IP is $GETIPADDR" [email protected]
        fi
    else
        curl $GETIPADDR >> $NOWIPADDR
    fi