#!/bin/sh
#
# Send an email message to notify about the external IP.
#
#set a file where we can store the ip
logfile=/var/log/extip
#to whom we send the email
recipient="my_account@provider.tld"
#if we are using an external smtp relay
smtp=relay.smtp.provider.tld
export smtp=$smtp
#put a nice sender address on the email
sender="IP Checker
#find the name of of this server
HOSTNAME=`hostname -a | cut -f1 -d" "`
# format a nice subject
subj="Change of IP on $HOSTNAME"
#now let's get the external ip
ip=`/usr/bin/lynx -dump 'http://whatismyip.org'`
#and create a message body
mesg="$HOSTNAME IP address on `date +%a' '%b' '%e' '%H.%M.%S' '%Z' '%Y` is $ip"
#see if the ip has changed
oldip=`cat $logfile`
#or if the last message has been sent more than one week ago
age=`find $logfile -mtime +7`
if [ ! "$oldip" == "$ip" ] || [ ! "$age" == "" ]
then
#keep the new ip into the logfile
echo $ip > $logfile
#send email about it
echo "$mesg" | mail -r"$sender" -s"$subj" "$recipient"
fi
exit 0
Of course, having an external web server, capable of running a little php script will allow us to replace http://whatismyip.org with our own webserver. The php script is really simple:
<?php
$ip = getenv('REMOTE_ADDR');
#$ip = $_SERVER['REMOTE_ADDR'];
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
' . $ip . '
</body>
</html>';
?>