Static subdomains for dynamic IP's
DynDNS offers a very popular service that allows people with dynamic IP addresses (addresses that change periodically) to have a static address on the internet that they can use personally or give out, allowing external access to specific services on their computer.
While this is a great solution for many, it has several drawbacks. The main one for me was that I wanted to use my own domain, bjornenki.com as part of the address.
Out of principle I did not want to have to pay extra each month to AT&T for a static IP address, so as a work-around, I took advantage of the DNS services included in my HostGator account along with the help of a tiny PHP file and two BASH scripts that're triggered to run periodically on my home server and web server.
Home server BASH script
The first BASH script runs on my home server, every few minutes. It access a web page on my site using "curl", that is rescricted with a password that's sent "in the clear". While this definitely isn't entirely secure, the risk factor is so low for this application that it's not really worth putting more thought into.
The web page the script accesses records the IP address of my home server (the device accessing the page) and writes it to two separate files in a restricted directory. The first file contains only the latest IP address of my home server, and the second is a log of each different IP it has had, just to make sure everything's working.
The curl command is very simple
curl http://www.bjornenki.com/dyn-ip/generate?login=password
Web server PHP file "generate"
if($_GET['login'] == 'password') {
$ip = $_SERVER['REMOTE_ADDR'];
$ipfile = "/homeip/homeip.txt";
$fh = @fopen($ipfile, 'w');
fwrite($fh, $ip);
fclose($fh);
} else {
header("HTTP/1.0 404 Not Found");
}
Web server BASH script
CURRENT_IP=`cat ~/www/homeip/homeip.txt`
LAST_CHECKED_IP=`cat ~/www/homeip/lasthomeip.txt`
if [ $CURRENT_IP != $LAST_CHECKED_IP ]; then
ping -c 1 $CURRENT_IP
if [ $? == 0 ] ; then
curl -u …lineXX=$CURRENT_IP http://whm.bjornenki.com/cgi/zoneeditor.cgi
if [ $? == 0 ]; then
echo $CURRENT_IP > ~/www/homeip/lasthomeip.txt
echo `date +%D` `date +%T` : $CURRENT_IP >>
~/www/homeip/dynamic-ip-history.txt
fi
fi
fi
I've had it in place for several months and it works perfectly. Even if I manually reset my router (triggering my ISP to send me a new dynamic IP), pinging the subdomain works immediately upon the dynamic-ip-update script running.
Let me know if you have any questions or need any help in getting this working for yourself.
