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.shCopy paste the following code block into this file:
CURRENT_IP=$(wget -q -O - http://www.whatismyip.com/automation/n09230945.asp)
if [ -f $IPFILE ]; then
if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
echo $CURRENT_IP > $IPFILE
mutt -s "My External IP is now "$CURRENT_IP email@example.com < $IPFILE
Edit "firstname.lastname@example.org" 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.shTo configure Mutt:
First, install Mutt:
sudo apt-get install muttFollow 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 .muttrcCopy paste the following into this file:
set imap_user = "email@example.com"
set imap_pass = "yourpassword"
set smtp_url = "smtp://firstname.lastname@example.org:587/"
set smtp_pass = "yourpassword"
set from = "email@example.com"
set realname = "IPCRONCHECK"
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
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" firstname.lastname@example.orgThen it's time to check the script works. Run:
./ipchangecheck.shThe 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 -eSelect nano, and add the line:
30 * * * * /home/user/ipchangecheck.sh
This makes it run once every half hour
ctrl+x to exit and save