Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using DNSExit on my Ubuntu server (Natty 11.04) and have installed the ipUpdate rpm file and did an apt-get install for chkconfig to enable the ipUpdate.

I'd like to get an email whenever the IP address changes, so that I can SSH into the box using PuTTY (because I can't unless I know the current IP address).

The current IP address is stored in /tmp/dnsexit-ip.txt, and I'd like to, whenever that file changes, to mail the contents of that file and the contents of /var/log/dnsexit.log (which contains a history of the IP changes) to my email address.

How can I accomplish this task? I'm thinking that a cronjob will be the solution, but I'm not sure how to do this.

share|improve this question
up vote 2 down vote accepted


Install sendEmail. It's a lightweight, command line SMTP email client. We'll use it to send emails from a script, using a Gmail account.

sudo apt-get install sendemail libio-socket-ssl-perl libnet-ssleay-perl

Create the script

Create a file named "" somewhere, for example in a "Scripts" directory in your home folder; make it executable, and open it up for editing.

mkdir -p ~/Scripts && touch ~/Scripts/ && chmod a+x ~/Scripts/ && gedit ~/Scripts/

Insert the following text in the file:


# Modify the following values!

  SENDERNAME="Computer" # This is the name that will show in the 'From' field. Purely esthetic.
  RECIPIENTNAME="Your Name" # This is the name that will show in the 'To' field. Also purely esthetic.
  GMAILADDRESS="" # This is your Gmail address.
  GMAILUSER="someemail" # This is your Gmail username, without the '' part.
  GMAILPASS="password" # This is your Gmail password.

# You can stop modifying here


if [[ $(find $DIR -mmin -2 -name $CURIP) ]];
        echo "$CURIP has been modified in the last two minutes."
        # Send an email
        sendemail -u "IP Address" -m "IP address has changed!" -f "$SENDERNAME <$GMAILADDRESS>" -t "$RECIPIENTNAME <$GMAILADDRESS>" -s $SMTPSERVER -xu $GMAILUSER -xp $GMAILPASS -a $DIR$CURIP $IPLOG

Once that's done, save and close the file.

Run the script periodically

We'll run this script every two minutes. Open up your crontab.

crontab -e

Add the following line to the bottom of the file:

*/2 * * * * bash ~/Scripts/

You're done!

If all goes well, you should now get email updates when the IP address of your machine changes.

share|improve this answer
Thank you @SirCharlo, I will give it a try and test with some mock files. Questions: 1. How to know what to put for the SenderAddress, and 2. What to set for the SmtpServer? – rishimaharaj May 25 '12 at 15:23
Good point. I modified my answer to use Gmail as the outgoing server; all you need is a Gmail account. – SirCharlo May 25 '12 at 15:45
A few fixes for your answer @SirCharlo: in the if statement, we've got some unused $EMAILADDRESS variables. To fix the first one, I added a declaration at the top, and for the second one, I changed it to $GMAILADDRESS. Now, I'm getting another error: SMTP-AUTH: Authentication to failed. Any ideas? – rishimaharaj May 25 '12 at 16:34
Fixed my mistake. The error you get means that either the GMAILUSER or the GMAILPASS variable is incorrect. Make sure that the username you specify is the first part of your email address (everything before the "@" sign). Make sure your password is correct, also. If that doesn't work, try replacing SMTPSERVER="" with SMTPSERVER="". – SirCharlo May 25 '12 at 16:44
You were correct! The email address was wrong -- I fixed it and it works. Question: Why did you decide to go the route where the gmail account sends the mail to itself, and not the way before where the server actually sends the mail? – rishimaharaj May 25 '12 at 16:54

If you have a mail installed (I use postfix ... i.e. sudo apt-get install postfix) you can run a bash script in cron every 5 minutes.

The script uses 'stat' to check if the file changed in the last five minutes (300 seconds) if so the $diff variable will equal 1.

Some thing like this should work:

filemtime=`stat -c %Y /tmp/dnsexit-ip.txt`
currtime=`date +%s`
diff=$(( (currtime - filemtime) / 300 ))

if [ $diff -eq  $x ]

    echo "Contents of dnsexit-ip.txt" >$EMAILMESSAGE
    cat /tmp/dnsexit-ip.txt >> $EMAILMESSAGE
    echo "Contents of dnsexit.log" >>$EMAILMESSAGE
    cat /var/log/dnsexit.log >> $EMAILMESSAGE

    sudo /usr/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
share|improve this answer
Thanks for the answer @paradrupal. I received a message in /var/mail/<username> with an error: /home/<username>/ line 7: [: missing `]'. How to fix this? – rishimaharaj May 25 '12 at 16:29
if [ $diff -eq $x ] this just says that if $diff is equal to 1 send the mail. Please check your syntax. – paradrupal May 25 '12 at 18:33
I'm currently getting this error: sudo: no tty present and no askpass program specified. – rishimaharaj May 27 '12 at 1:57
Sorry holiday weekend ... What mail program are you using? You may not need to use sudo. Can you sudo from the command line? Are you a privileged user? – paradrupal May 29 '12 at 13:00
Both Postfix and SendEmail are installed. My user account is an admin and can sudo. – rishimaharaj May 29 '12 at 15:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.