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:  

Copy paste the following code block into this file:


CURRENT_IP=$(wget -q -O -
if [ -f $IPFILE ]; then

if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
mutt -s "My External IP is now "$CURRENT_IP < $IPFILE

Edit "" 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
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 = ""
set imap_pass = "yourpassword"

 set smtp_url = "smtp://"
set smtp_pass = "yourpassword"
set from = ""
set realname = "IPCRONCHECK"

set folder = "imaps://"
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" 
Then it's time to check the script works. Run:
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/

This makes it run once every half hour

ctrl+x to exit and save


1 comment:

  1. doesn't seem to be accepting automated requests... I had some luck with using curl .... 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.