External IP change notification

Having dynamic IP addressing from your Internet Provider (ISP) could give you some trouble; especially when you need to remotely accessing machines;

I had to write a little script that regulary based checks my external IP; And if it change then sending to me an email.
Schedule it into your crontab and you are done.

Below, the script :

 

#!/bin/bash

###########################################
# script that check your external IP address
# if the one has changed to compare with the provious one
# and an email is generated and sent; Otherwise nothing.
#
# Nicolas.B - 04032013
##########################################
#
# File log used locally : myip.log 
#
#
#
############
# variables

mailfrom="sender_adressemail@domain.com"
rcpt="recipient_adressemail@domain.com"
subject="My external IP"
###########


############################
# check if wget is present

command -v wget >/dev/null 2>&1 || { echo "I require wget but it's not installed. Aborting." >&2; exit 1; }
command -v mail >/dev/null 2>&1 || { echo "I require mail but it's not installed. Aborting." >&2; exit 1; }

##########
# retrieve my external ip

wget --default-page=checkip.html http://checkip.dyndns.com/ 2>1 /dev/null
check=$(cat checkip.html | awk '{print $6}'|cut -d"<" -f1) if [ -f myip.log ]; then readip=$(cat myip.log) else wget http://checkip.dyndns.com/ 2>1 /dev/null
cat checkip.html | awk '{print $6}'|cut -d"<" -f1 > myip.log
readip=$(cat myip.log)
fi

###########

if [ "$check" = "$readip" ]; then 
echo ""
echo "this is the same IP: $check"
echo "email not sent to $rcpt"
echo ""
else

wget http://checkip.dyndns.com/ 2>1 /dev/null
cat checkip.html | awk '{print $6}'|cut -d"<" -f1 > myip.log


###########
# send the email
mail -r $mailfrom -s "$subject" $rcpt < myip.log

fi


if [ -f checkip.html ]; then
rm checkip.html
else
echo ""

fi

Leave a Reply