Monday, 1 February 2010

A cronjob script to e-mail you upon external IP address change


A while ago I was researching the implementation of a cronjob to e-mail me upon external ip change of a media server running MythTv in the house so as to be able to access MythWeb externally without issue. In any case, a solution presented itself in the form of a useful little bash script put together with the help of various sources (Which I can no longer for the life of me find) and a command line e-mail program called Mutt.

The implementation of this requires the creation of a small script, the installation and configuration of Mutt, and a Gmail account for sending the actual e-mail (though a native e-mail server, or anything handling imap will also do).

Howto after the break.


To create the script:  

gedit ipchangecheck.sh
Copy paste the following code block into this file:

#!/bin/bash

IPFILE=~/ipaddress
CURRENT_IP=$(wget -q -O - http://www.whatismyip.com/automation/n09230945.asp)
if [ -f $IPFILE ]; then
KNOWN_IP=$(cat $IPFILE)
else
KNOWN_IP=
fi

if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
echo $CURRENT_IP > $IPFILE
mutt -s "My External IP is now "$CURRENT_IP youremail@blah.com < $IPFILE
fi


Edit "youremail@blah.com" to reflect the address you want to be mailed by this script, then save and quit.

This file them needs to be made executable:
chmod 700 ipchangecheck.sh
To configure Mutt:


First, install Mutt:
sudo apt-get install mutt 
Follow all the settings as default on the installation and select internet on the appropriate option it brings up.

To configure, you must create a file called .muttrc in /home
gedit .muttrc 
Copy paste the following into this file:

set imap_user = "yourgmailaccount@gmail.com"
set imap_pass = "yourpassword"

 set smtp_url = "smtp://yourgmailaccount@smtp.gmail.com:587/"
set smtp_pass = "yourpassword"
set from = "yourgmailaccount@gmail.com"
set realname = "IPCRONCHECK"

set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"

set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates

set move = no


Save and exit.

Now is a good time to test Mutt, to do so try e-mailing yourself using:
mutt -s "My External IP is now blahblahblah" youremail@blah.com 
Then it's time to check the script works. Run:
./ipchangecheck.sh
The first time this is run you should be e-mailed your current ip. If you then run it again you should be sent nothing, as your address has not changed.

If either or these steps fails, please drop me a comment and i'll attempt to help.

To add this script to crontab so  it is run once every half hour:  

crontab -e
Select nano, and add the line:

30 * * * * /home/user/ipchangecheck.sh

This makes it run once every half hour

ctrl+x to exit and save

Done!

1 comment:

  1. Whatismyip.com doesn't seem to be accepting automated requests... I had some luck with using curl icanhazip.com .... I'm still working on the rest of this. Otherwise this is a great start for me. I like the use of the crontab to get it every 30 minutes.

    ReplyDelete