Scheduling a daily Cron task in Debian to check external IP

I've been using DynDNS services for a while to have a human-readable address to access my home services. But recently, I realized how much cooler it would be to point a domain or sub-domain to my home network. To do this though, I would need a relatively non-volatile external IP address for my home network. I'm not sure what the frequency of which my ISP changes this, so I figured it would be a good idea to start logging this data.

You will need a few things for this to work. The first is a site which displays the IP address of the machine requesting the page. I built one for this, available at ip.thomashunter.name, but you may want to use a service with a guaranteed uptime, or host one yourself. Here's the PHP sourcecode for the script (it's super simple):

<?php
echo $_SERVER['REMOTE_ADDR'];
if (isset($_GET['newline'])) {
    echo "n";
}

You will also need to create a script to grab that external IP address, and put it in the location to have Cron execute it. If you would like to run this every day, place the script in /etc/cron/daily. Here is a script I created for the task (it requires cURL to be installed, although the same could be achieved with wget):

#!/bin/sh
date +"%Y-%m-%d: " | tr -d 'n' >> /var/log/external-ip.log
curl http://ip.thomashunter.name?newline -s >> /var/log/external-ip.log

You may also want to create the log file and give it writeable permissions (if you want other users to be able to run the process that is), although it should be created automatically by bash.

Whenever you want to view the status of the IP address changes (e.g. in a couple weeks), you can ran the following command:

cat /var/log/external-ip.log

Example output:

2011-07-31: xxx.xxx.xxx.xxx
2011-08-01: xxx.xxx.xxx.xxx
2011-08-02: xxx.xxx.xxx.xxx
2011-08-03: xxx.xxx.xxx.xxx
Thomas Hunter II Avatar

Thomas is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.