Bjørn Enki’s web design blog

Static subdomains for dynamic IP's

This post explains how I recently created a DynDNS type service for myself so that my clients and I may access my home server remotely using a static address as a virtual subdomain of bjornenki.com.

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.

Posted May. 16, 2009 at 10:32 pm under: linux, tools

Comments

Mikuz, Jan. 30, 2012 at 4:11 pm
Unfortunately I was unable to use whm, too. Didn't find a workaround for that. BUT I figured out something nice. You can use the DNS Zone Editor to redirect your subdomain to your free DynDNS or No-Ip subdomain CNAME. Still got to type all those captchas thou..
Mikuz, Jan. 30, 2012 at 11:49 am
Seriously got to try this one out. I have all my computers hanging on no-ip.org It's not a bad address but I really would prefer my own domain. Plus I hate to write all those captchas every month :)
Jose, Aug. 12, 2010 at 4:57 pm
Thanks for the response. I see you're using curl to update via whm. I don't have access to whm with my hosting provider, but can access the Simple/Advanced DNS controls from within my user cpanel. The URLs are http://mydomain:2082/frontend/x3/zoneedit/simple.html and http://mydomain:2082/frontend/x3/zoneedit/advanced.html. Any ideas what I would need to modify the curl URL to be? And any chance for a bit more detailed directions starting from the beginning, when you get time? I would greatly appreciate it! Thanks!
Bjorn Enki, Aug. 12, 2010 at 4:13 pm
Hi Jose, I apologize for the confusion, but that's exactly what this setup does. The "Web server BASH script" portion effectively submits the exact same form that's in Cpanel to manage DNS entries. The only difference is that it posts using a curl command (automatically) rather than through the browser, and can use variables (the new dynamic IP) that don't have to be entered manually.
Jose, Aug. 12, 2010 at 10:28 am
Is there anyway to get this to update Cpanel's stored dns entry for a subdomain?
Leave a response!