Here is a little script that queries
http://checkip.dyndns.org/ and compares it to my Routers WAN IP address. If there's no change, the script exits. If the IP address doesn't match the result returned from DynDns, an email is sent to a Gmail address using sSMTP mail server.
I used this tutorial
http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/ to set up sSMTP. I highly recommend this site.
This is my ssmtp.conf file. It's slightly different to the tutorial. Suggestions for improvement would be gratefully accepted:
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail
# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=sheeva
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
root=myemailaddresshere@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
AuthUser=myemailaddresshere # (without@gmail.com)
AuthPass=*************
Three files will be created in the root of my user directory (in this case badcam):
iplog.txt - This file is blank initially. The script will use this for storing the current WAN IP address which is compared against the DynDns.org websites CheckIP.
whereami.txt - This file is used to store the data which will be sent as an email
whereami.sh - This is my Bash script, which is as follows:
#!/bin/bash
# A script for sending myself an email containing my WAN IP Address
get_ip=`wget -q -O - http://checkip.dyndns.org/ | grep -Eo '[0-9\.]+'`
if [ "$get_ip" = "$(cat ./iplog.txt)" ]; then
echo "No need, I'm out of here..."
exit;
else
echo "The IP has changed! Sending an email..."
echo "SUBJECT: My External WAN IP Address has changed" > ./whereami.txt
echo "FROM: Badcam's Island in the Net" >> ./whereami.txt
date >> ./whereami.txt
echo " " >> ./whereami.txt
echo "$get_ip" >> ./whereami.txt
echo "$get_ip" > ./iplog.txt
/usr/sbin/ssmtp -oi myemailaddress@gmail.com < ./whereami.txt
fi
Then I used this tutorial
http://www.nixtutor.com/linux/sending-email-alerts-through-cron/ to set up a Cron Job to automatically run every 15 minutes.
My Crontab -e file looks like this, where the */15 means run every fifteen minutes:
# m h dom mon dow command
MAILTO="myemailaddresshere@gmail.com
*/5 * * * * /home/badcam/whereami.sh
~
If anyone can find something I can improve here, or make clearer, please let me know.