Search This Blog

Sunday, November 06, 2011

IP Changing notification

Because sometimes the dyndns updater goes to hell, a little crontab script executed every couple of hours it's a life saver:

# Send an email message to notify about the external IP.

#set a file where we can store the ip
#to whom we send the email
#if we are using an external smtp relay
export smtp=$smtp
#put a nice sender address on the email
sender="IP Checker <ipcheck@server.tld>"
#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 ''`
#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" == "" ]
 #keep the new ip into the logfile

 echo $ip &gt; $logfile
 #send email about it
 echo "$mesg" | mail -r"$sender" -s"$subj" "$recipient"

exit 0

Of course, having an external web server, capable of running a little php script will allow us to replace with our own webserver. The php script is really simple:

$ip = getenv('REMOTE_ADDR');
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
' . $ip . '

No comments:

Post a Comment

Blog Archive