Get Email/SMS notifications when your IP changes (Linux)

A few days ago, I decided to take a break from my home office and bang on the keys at the local coffee shop.  Like a responsible and security conscious developer, I have a secured VPN tunnel to my home network that helps keep prying eyes out of my network.  I get my grande venti caffeine  sludge, find a nice seat outside, connect to my VPN when…  “Connection failed.  No server at address.”

It doesn’t happen often, but my ISP had changed my external IP address.  Not normally a huge deal if I know in advance, but being stuck on public WiFi with no access to my home network, I’m dead in the water.  For anyone with a home network with externally accessible services, knowing your IP the only way to access those services from anywhere else.  Having a monitor to notify you when an IP changes is invaluable.  This is not the only method, may not be the easiest, may not be the best, but it’s a relatively simple method which works very well for me.

Postfix

Postfix is a simple mailer program for Linux.  It lends itself well to sending email messages from the command line and bash scripts which we’ll be setting up.  If Postfix is not already installed as a dependency to another service, follow these simple instructions to set up an Internet Site with default configuration.  If Postfix has been installed and configured for another service (in my case with Nagios) follow these instructions to reconfigure the Postfix service for this process.  Make sure you’re not taking down notification services for another app by reconfiguring Postfix setup.  If you’re unsure, use another machine or set up a new server instance to avoid conflict.  For my network, I have my Nagios email notifications turned off, so no conflict.

Gmail Relay

I’m assuming most folks don’t have an SMTP relay setup on their home networks.  Those who do probably aren’t reading his tutorial anyway.  Thankfully, it’s easy to setup Gmail as an SMTP relay for Postfix.  If you don’t already have a Gmail account, get one.  Whether or not you use it for personal email, the services you get from their API and mail are unmatched.  RT Camp has a great tutorial to set up Gmail as your Postfix relay:   Configure Postfix to Use Gmail SMTP on Ubuntu

Make sure to test the service before you move on:

echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com

Cronjob

Now that the notification method is set up, we need to create the service that checks for IP changes.  Since this service doesn’t need to be closely monitored, we can use the predefined cron job folders.  We’ll create a bash script to check our IP address.  By placing it in once of the cron folders it will be run at the interval specified by the folder name.

/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

Once you’ve selected the appropriate folder, create a new bash script.

sudo nano /etc/cron.hourly/ip-check.sh

The script itself is modified version of the script presented by  Dan Rossiter in his post here:  Accurately Track Your Dynamic IP With Text Messages  His setup involves creating a Perl SMS service that can be run from the command line.  Very cool, but a little more involved than I wanted get with my solution.  I remembered that most SMS providers also have an email gateway available which delivers an email as an SMS message.  This allows us to get away with SMS notifications using only email and our Postfix setup.  Here’s the modified script I’m using.  (Replace the highlighted properties with your own values)

#!/bin/sh
 
# Before using, home directory references must be modified to match invoking user.
# Additionally, run touch on ~/.ip-cache prior to first run to avoid issues.
 
cache='/etc/.ip-cache'
prev=`/bin/cat $cache`
current=`/usr/bin/wget -qO- http://ipecho.net/plain`
 
# trap possible wget error
if [ $? != 0 ]; then
   exit 1;
fi
 
# new IP found
if [ "$current" != "$prev" ]; then
   echo "$current" | mail -s "Public IP Address Changed" 1234567890@vtext.com
   echo "$current" | mail -s "Public IP Address Changed" emailaddress@gmail.com
 
   # SMS was successfully delivered to Google Voice
   # (impossible to determine whether SMS was delivered to phone)
   if [ $? -eq 0 ]; then
      /bin/echo "$current" > "$cache"
      exit 0
 
   # SMS send failed
   else
      exit 1;
   fi
 
# old IP still present
else
   exit 0;
fi

Most mobile providers have an email gateway for text messages.  I’m with Verizon so my SMS gateway is <number>@vtext.com.  Here’s a list of some of the common gateways, but please check with your provider for accuracy.

Metro PCS ##########mymetropcs.com
T-Mobile ##########tmomail.net
Verizon Wireless ##########vtext.com
AT&T ##########txt.att.net
Sprint PCS ##########messaging.sprintpcs.com
Nextel ##########messaging.nextel.com
Cricket ##########sms.mycricket.com
US Cellular ##########email.uscc.net
Cingular (GSM) ##########cingularme.com
Cingular (TDMA) ##########mmode.com

In order to test the script, run the bash from the command line.

bash /etc/cron.hourly/ip-check.sh

If all goes well, you should receive an email and an SMS message with the new IP address.  This script caches the current IP address in the /etc/.ip-cache file.  If you need to test this process multiple times, clear the contents of this file before running each test.

37463098
This certainly doesn’t solve all issues related to IP address change, but it’s helpful to know the source for a failed connection.  Hope this helps some of the rest of you.

As always, thanks for reading.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.