Get email when Public IP address changes using RasberryPi

by | Jan 22, 2015 | Debian Wheezy (7)

If you can’t spend thousands for a static IP, Try out this script which sends out an email alert whenever Public IP address changes. In a situation where you need a VPN solution without spending thousands on a static IP no matter if you are a HOME user or a Small Business owner, all you need to have is a RasberryPi and a decent Small Business VPN Router. For this article I used a Cisco RV110w VPN Router.

Prerequisites
RasberryPi
Decent Small Business VPN Router ( I used CISCO RV110w Small Business VPN Router )

We are gonna use RasberryPi and an automated script running on it to extract the Active Public IP to be used when configuring a VPN client to connect a remote VPN server which would be accessible no matter where you are, you would be able to access your LOCAL home/Office LAN as long as there is Internet availability.

First you need to setup a outgoing email server on your RasberryPi so that it send you email alerts. Check this article for that setup.
Next would be to copy this script onto RasPi and create a CRON Job at a given interval for it to poll for public IP and alert you via email whenever Public IP changes.

Create a file and copy this script

# vi /tmp/trigger_ip_change.sh

#!/bin/bash
IPFILE=~/ipaddress
CURRENT_IP=$(dig TXT +short o-o.myaddr.l.google.com | sed ‘s/”//g’)
if [ -f $IPFILE ]; then
KNOWN_IP=$(cat $IPFILE)
else
KNOWN_IP=
fi

if [ “$CURRENT_IP” != “$KNOWN_IP” ]; then
echo $CURRENT_IP > $IPFILE
mail -a “From: RASeServer <testuser@gmail.com>” -s “Changed Public IP on $(date)” raspi@gmail.com < $IPFILE
logger -t ext_ipcheck — IP changed to $CURRENT_IP
else
logger -t ext_ipcheck — NO IP Change
fi

Line 2: Creates a file to store the value of Public IP in /root/ipaddress.
Line 3: Pulls the current Public IP from “checkip.dyndns.org” of your ISP/Internet Provider and stores that in CURRENT IP variable.
Line 4: Checks if there is a regular file created in /root/ipaddress.
Line 5: Takes the value from IPFILE for comparing it to KNOWN IP.
Line 10: Compares the value of CURRENT IP and KNOWN IP and if different, fires up an email alert with the changed IP, otherwise no alert.

To better understand HOW the script executes, run it in a Verbose Manner and edit according you requirement.

Running for 1st Time, it sends an email alert
Output: 

root@viz-raspi-st:~# bash -x trigger_ip_change.sh
+ IPFILE=/root/ipaddress
++ wget -q -O – checkip.dyndns.org
++ sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//’
+ CURRENT_IP=90.124.169.232
+ ‘[‘ -f /root/ipaddress ‘]’
+ KNOWN_IP=
+ ‘[‘ 90.124.169.232 ‘!=’ ” ‘]’
+ echo 90.124.169.232
++ date
+ mail -a ‘From: RASeServer <testuser@gmail.com>’ -s ‘Changed Public IP on Fri Jan 30 18:14:15 GMT 2015’ raspi@gmail.com

Running 2nd Time, it will only alert IF the Public IP has changed otherwise it doesn’t. This time it stops just after comparing Values in CURRENT IP & KNOWN IP.
Output:

root@viz-raspi-st:~# bash -x trigger_ip_change.sh
+ IPFILE=/root/ipaddress
++ wget -q -O – checkip.dyndns.org
++ sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//’
+ CURRENT_IP=90.124.169.232
+ ‘[‘ -f /root/ipaddress ‘]’
++ cat /root/ipaddress
+ KNOWN_IP=90.124.169.232
+ ‘[‘ 90.124.169.232 ‘!=’ 90.124.169.232 ‘]’

Now lastly create a CRON Job to run every 15 mins or whatever frequency you like to check the Public IP and alert only when it has changed.

Make the script executable before adding it to the crontab.

# chmod +x /root/trigger_ip_change.sh

# crontab -e

# Add this line in CronTab
# m h dom mon dow command
*/15 * * * * /root/trigger_ip_change.sh

* It runs every 15 mins “*/15”

That’s it and you didn’t even need DymanicDNS service or an expensive STATIC IP.

If you encounter any errors during the process, let us know via Contact Us and will try our best to help.

Related Articles….