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.

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/

CURRENT_IP=$(dig TXT +short | sed ‘s/”//g’)
if [ -f $IPFILE ]; then

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

Line 2: Creates a file to store the value of Public IP in /root/ipaddress.
Line 3: Pulls the current Public IP from “” 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

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

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.

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

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/

# crontab -e

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

* 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….