"Personal thoughts, ramblings, and nonsense from Drew, himself."
This post was written on September 9, 2008 at, or around 8:38 pm by Drew. This post is composed of 433 words from the English language and currently has 2 comments to its name. Additionally, this article is tagged under DNS, Networking, Shell Scripting, UNIX/Linux and you can trackback to this article using this link. This post was last updated on Oct 23, 2008. Enough talk, carry on.
You ever try connecting to your system at home, using a domain name (that has been setup using Dynamic DNS), only to find out that your ISP has, yet again, changed the public IP address to your house? This is very common with DSL setups, but still is a factor with other internet connections such as FIOS, Cable (also known as High Speed Internet, and most definitely dial-up (since dial-up isn’t really used much anymore, I’m not covering that). Of course, there’s those Dynamic DNS providers, which I also use. For example, there’s no-ip.com; with Dynamic DNS applications for Windows, Linux, and Mac OS, however, they do not really work all that well with my ISP. My ISP public IP changes a good 12 times a day (no kidding!). No-IP just can’t keep up with my public IP changes, which sucks, cause I pay for that. I’ve written a small shell script to at least notify you when your public IP address has changed, so that, if your dynamic DNS has not propogated, you will at least be able to connect using the IP address.
What we are going to do is query the website What Is My IP Address, return the IP that we get from the website, check to see if the IP address is different than what we already have on file, and email the new IP address, if we need to.
There are many other ways to do this, and this I found the most simple way to do so. There are also cleaner ways to do this, but again, I’m going about the easiest way possible; it works. So, without further ado, here’s the script in its entirety:
Shell Script
#!/bin/sh
## VARIABLES
IP=`links http://whatismyipaddress.com | awk '/Your IP address is/ { print $5 }'`
SCRIPTSHOME=/scripts
MAILTO=user@example.com
## MAIN SCRIPT
clear
echo "*****************************"
echo "** Dynamic DNS IP Notifier **"
echo "*****************************"
echo -n "Current detected IP address: $IP"
echo ""
if [ ! -f "$SCRIPTSHOME/ips.log" ]; then
echo "ips.log NOT found. Creating file."
touch $SCRIPTSHOME/ips.log
else
echo "Sanity checks passed. Continuing."
fi
echo -n "Checking if this is a new IP"
if [ "`cat $SCRIPTSHOME/ips.log`" != $IP ]; then
echo "...IP HAS CHANGED!"
echo $IP > $SCRIPTSHOME/ips.log
echo -n "Emailing the current IP"
mutt -s "** IP ALERT ** - New Dynamic IP!" $MAILTO <<< $IP
echo "...DONE"
else
echo "...IP HAS NOT CHANGED!"
fi
echo ""
echo "...Exiting"
exit 0
All you need to do is change the $SCRIPTSHOME variable at the top to where this shell script will reside and the $MAILTO variable, which will need to have the email address of whom the recipient will be of the alert email (most likely, you). When this script first runs, it should create an ips.log file in the same directory as the script; this will hold the current known public IP. We are about to set up the cron job to do the interval checking of the current public IP address.
NOTE: I have decided to use Mutt as the email client to use. This is because I have set this up with my Google Apps account. It’s really easy to set up and once it is set up, works flawlessly. Most people might want to use the mail command. To use the mail command, make sure you have the mailx package installed and make sure that sendmail is configured correctly (not discussed here). You can alter the syntax of my script to use your email client of choice, but if you need some help, let me know and I’ll be more than happy to help out.
The Cron Job setup is faily easy. You will need to run crontab -e to edit your cron jobs. From here you will need to add the line below (the one that starts with the asterix), to create the job. The numbers in the beginning tell when the job will take place:
Cron Job
crontab -e
*/15 0-23 * * * /scripts/currentip.sh
Basically this job will run every 15 minutes every hour of every day/week/month all year. Change the /scripts/currentip.sh to where you put the shell script from above. Now save your document (the crontab you are currently editing). Now that it is saved, the script will start running every 15 minutes. You should get the initial email in about 15 minutes, as the ips.log should not exist yet, unless you have already manually run the script.
This should work for most people, however, I’m pretty sure I’m going to get a few comments (if any) from people that have issues with not recieving any email. First of all, make sure that standard email works with the client you are using to send email. Secondly, make sure that sendmail is working properly (which goes with what a just previously said). I hope this script comes in handy for some people. I basically made it for myself, but decided to share for those that might have experienced my issue.
This is great, I can finally complete my dns updater.
Also myip.dnsomatic.com or how ever you spell it just gives you the ip address alone.
Had to change
awk ‘/Your IP address is/
to
awk ‘/Proxy Server IP address:/
Check in a terminal using
links http://whatismyipaddress.com
to see what text you need.
Thanks for this, very helpful
Note: If this is your first time commenting on my site, there will be a delay, as I have to approve your comment.
Ajax AOL/AIM Apache Applications Architecture Career Case Projects CentOS CSS Debian Design File Systems Google Hacks Hardware Humor JavaScript Life Management Movies Networking Open-Standards Personal PHP Programming Registry Samba Security Shell Scripting Software Sys Admin Tech The Notebook Tips UNIX/Linux Virtualization VMWare VPC (MS Virtual PC) Walkthrough Web Apps Windows Work Workaround XHTML XHTML 2.0