Since I’ve moved I have a smaller DSL ISP that like to keep short dhcp leases. I found out quickly that on each router reset I get a new IP Address. Over the last few weeks it’s gotten even better… Now intermittently my PPPoE loses connection and is forced to reconnect. (My TV is on the same line and it’s been flaking out too so I know it’s not my router… It could be my modem so I’ll keep an eye on it.
Because I like to connect to my home PCs from anywhere, I need to know my IP address and when it changes. Last night, while watching to see if the tornados were coming to my house, I sat down and began to write a shell script that would detect my current IP and see if it has changed.
The script is pretty simple really… Here is what it does:
- Check to see if there is an IP address already recorded
- If yes – get and md5sum on the file and then move it to another file
- Get my current external IP address and save it to file
- Caclulate the md5 on that new file
- Check the two md5 sums and see if they match
- Do they match? Do nothing
- They don’t match? Lets send an e-mail to let me know
I also had a couple of bottlenecks. The first is that I couldn’t get sendmail to send an e-mail from my box… To get around this, I made a PHP script and put it on a web server. Now when I go to send the e-mail I call ‘wget -qO- “myurl?key=1&ip=${MYIP}” | grep cat.
The second concern is that I am calling an external page to find my external IP address. I’m afraid they might think I am spamming them so I created another php script to return my external IP address and posted it on my web server as well.
Now everything is working and I’ve setup an hourly cron job to run the script.
The shell script:
#!/bin/sh
cd ~
# curl -s checkip.dyndns.com | grep -Eo “[0-9]+.[0-9]+.[0-9]+.[0-9]+”
# curl -s www.whatismyip.com | grep -Eo “[0-9]+.[0-9]+.[0-9]+.[0-9]+”
# curl -s www.info66.net/whatsmyip.php | grep -Eo “[0-9]+.[0-9]+.[0-9]+.[0-9]+”
# MYIP=`curl -s www.ipchicken.com | grep -Eo “[0-9]+.[0-9]+.[0-9]+.[0-9]+”`
MYIP=`curl -s www.info66.net/whatsmyip.php | grep -Eo “[0-9]+.[0-9]+.[0-9]+.[0-9]+”`
MD5_LST=”"
OLD_IP=”"
if [ -f ExtIp.txt ]; then
MD5_LST=`md5sum ./ExtIp.txt`
OLD_IP=`cat ./ExtIp.txt`
mv ./ExtIp.txt ./PrevExtIp.txt
fi
echo $MYIP > ./ExtIp.txt
MD5_NOW=`md5sum ./ExtIp.txt`
echo ” Ip: ${MYIP}”
echo ” Now: ${MD5_NOW}”
echo ” Lst: ${MD5_LST}”
if [ "${MD5_NOW}" != "${MD5_LST}" ]; then
echo “IP Address Changed to ${MYIP} from ${OLD_IP}”
wget -qO- “http://www.webserver.web/email.php?key=abcdef&ip=${MYIP}” | cat
fi
whatsmyip.php
<?php
$ip = "";
$ip = $_SERVER['REMOTE_ADDR'];
echo "<h1>Your IP Address Is: " . $ip . " </h1>";
?>
email.php
<?php
$key = "";
$ip = "";
$to = "";
if ( !empty($_GET['ip']) ) {
$ip = $_GET['ip'];
$message = "My new IP is " . $_GET['ip'] . "...";
} else {
$message = "This is a test";
}
if ( !empty($_GET['to']) ) {
$to = $_GET['to'];
} else {
$to = to@email.com;
}
if ( !empty($_GET['key']) ) {
$key = $_GET['key'];
} else {
$key = “”;
}
$subject = “My New IP Address: “ . $ip;
$headers .= “From: from@address.comrn”;
$headers .= “Reply-To: from@address.comrn”;
$headers .= “Return-Path: from@address.comrn”;
$headers .= “X-Mailer: PHP/” . phpversion() . “rn”;
$headers .= “MIME-Version: 1.0rn”;
$headers .= “Content-Type: text/html; charset=”iso-8859-1″n”;
$headers .= “X-Priority: 1 (Higuest)n”;
$headers .= “X-MSMail-Priority: Highn”;
$headers .= “Importance: Highn”;
if ( $key == “abcdef” ) {
// This $key is just a safety check so people can’t
// use this as span so easily
$status = mail($to, $subject, $message, $headers);
} else {
$status = 0;
}
if ($status) {
echo “Successrn”;
} else {
echo “Failedrn”;
}
?>