This simple bash script checks your current public IP and compares it to the previous days public IP (depending on cron job). Email alert is sent if IP has changed.

Credit goes to Ronny Bull – http://ronnybull.com/2011/08/10/bash-dynamic-public-ip-address-monitor-script/

Pre-req – install curl and postfix

apt-get install curl
apt-get install postfix

Bash script:


#!/bin/bash

#Script to report public IP address change
#By: Ronny L. Bull

TO="email@address.com"
FROM="alerts@address.com"

#The file that contains the current pubic IP
EXT_IP_FILE="/path/to/ipaddressfile"

#Get the current public IP from whatsmyip.com
CURRENT_IP=$(curl http://ifconfig.me/ip)

#Check file for previous IP address
if [ -f $EXT_IP_FILE ]; then
KNOWN_IP=$(cat $EXT_IP_FILE)
else
KNOWN_IP=
fi

#See if the IP has changed
if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
echo $CURRENT_IP > $EXT_IP_FILE

#If so send an alert
echo "Subject: The IP Address at home has changed
The IP address at home has been changed to $CURRENT_IP" | mail -s ${FROM} ${TO}

logger -t ipcheck -- IP changed to $CURRENT_IP
else

#If not just report that it stayed the same
echo "Subject: The IP Address at home is the same
The IP address at home stayed the same $CURRENT_IP" | sendmail -f ${FROM} ${TO}
logger -t ipcheck -- NO IP change
fi

Make the bash scrip above executable:


chmod +x filename

Add a cron job to send the IP address daily – http://www.csgnetwork.com/crongen.html

*Ronny’s script was updated with the following line – CURRENT_IP=$(curl http://ifconfig.me/ip)

Written by Matt Cooper
Hi, I'm Matt Cooper. I started this blog to pretty much act as a brain dump area for things I learn from day to day. You can contact me at: matt@linuxtutorial.co.uk.