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.
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
# Where will the mail seem to come from?
# The full hostname
# 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
AuthUser=myemailaddresshere # (email@example.com)
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:
# 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..."
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 firstname.lastname@example.org < ./whereami.txt
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
*/5 * * * * /home/badcam/whereami.sh
If anyone can find something I can improve here, or make clearer, please let me know.