Google Site SearchFN Site Search FN Blog Login FN Blog Login
Site Navigation:
 
 

Dynamic DNS (DyNS) Update Behind Router

by Lim Chong Guan on May 24, 2004 (UPDATED Jun 21, 2004)

It has been brought to my attention that BNL IP Address Checker is giving some problem.  So I have updated the program to use yet another fine IP addressing return service.  Please visit http://serve.me.nus.edu.sg/limcg/getip.html for the updated program.  Sorry for the inconvenience caused.

My Problem

Like most broadband services, my home broadband ISP network IP is dynamic, meaning, my IP doesn't always stay the same. If I want to have a means to access my home computer easily, say from work, I will need to subscribe to one of the many free Dynamic DNS services available on the internet. One of my personal favourite is DyNS (www.dyns.cx). They offer simple no-fuss sign-ups and very intuitive, clean and easy to use web-interface. And they have made most of their services free. Really neat! To be able to made use of DyNS services the very first thing you need to do is to sign yourself up with an account with them. That said, I am basing this article on updating DyNS DDNS, specifically.

Now, in the beginning, poor me only had a little Linux box that connected directly to the cable modem at home. I used this really nice IP updater program by Angus Mackay, ez-ipupdate. Things were simple and I had no problem keeping my DyNS host in sync with my dynamic IPs. However, as my family members added more PCs to the broadband connection, a router was added. Things became a little more complicated as my fedora box now sits behind a router. Ez-ipupdate didn't work that well behind the router. So I thought I'd just script up a simple program to do that trick. A simple program that will work with most routers.

 

Simple Solution

When I did some researches I come across serveral web services that return the router's external IP when I issue a query from my machines behind the router. I cannot thank this site, Brookhaven National Lab (BNL) , enough for providing such an informative IP Address Checker (http://www.bnl.gov/itd/webapps/checkip.asp) service. I find that this service gives more accurate IP information and works with most routers and even corporate proxy/firewall servers.

With all these wonderful free tools and services at hand, things look brighter already. My program, getip, is very simple. Everytime it is executed it does the below main steps:

  1. Query IP Address Checker for the latest external IP
  2. If this is the first time you run getip,
    • write the latest external IP into a local compare file for future comparison
    • update DyNS with the external IP
  3. Else
    • if the latest external IP is same as that stored in the local compare file
      • do not update DyNS (since IP has not changed)
    • else
      • write the latest external IP into the local compare file for future comparison
      • update DyNS with the external IP

 

Installation and Running the Program

Now, let's just get to the main program. The Fedora/Linux box will need to have perl installed as getip is coded in perl programming language. Also I use lynx text web browser with the -dump option to initiate an IP query to IP Address Checker.

[cray@homefc1]# lynx -dump "http://www.bnl.gov/itd/webapps/checkip.asp"

This returns a simple text reply from the IP Address Checker which makes our IP address mining job easy. The router external IP is the dot formatted IP after "Your IP Address is: " eg. 111.111.135.102 in the below example.

-------------- snipped -----------

Your IP Address is: 111.111.135.102
Firewall/Proxy Server is:1.0 lava (NetCache NetApp/5.3.1R4D6)
Firewall/Proxy Server IP Address is: 111.111.20.9

-------------- snipped -----------

Both perl and lynx packages are included in Fedora Core 1 (FC1) and in most other Linux distributions I have used. Please install them if you have not done so. You can install them using apt-get, yum or from the FC1 installation CDROMs.You do not need to know perl language at all to use the program. The below simple steps are required to run the program successfully.

Step 1

Download getip. It is best to place the script in a directory listed in the path environment variable. I suggest creating and copying the program into ./bin sub-directory in your own home directory eg. /home/cray/bin/getip.

Step 2

Edit getip (with plain text editors like vi or pico). On Fedora/Redhat Linux systems I assume perl executable is "/usr/bin/perl". If you have installed perl in some other directory please edit the path to perl on line 1 accordingly. Update your DyNS username, password, host.

33 my $username="myusername"; # key in your DyNS username here
34 my $password="mypassword"; # key in your DyNS account password
35 my $host="myhost.dynx.cx"; # key in your DyNS host.domain
36 my $script_dir="$ENV{'HOME'}/bin/"; # directory where the script resides (with an ending "/")

As I mentioned in step 1, the default directory to run getip is the "./bin" subdirectory in your home directory eg. "/home/cray/bin". You do not need to change the $script_dir variable setting if you are running it from the default directory. Otherwise, you can always replace it with the absolute path of the directory you copied the script to (remember the ending "/") eg.

my $script_dir="/home/cray/dyns_script/";

Step 3

cd to the script directory and chmod it into an executable file:

[cray@dyns]# chmod 755 getip

Step 4

Done! You're ready to update DyNS (note that if you are issuing the below commands from other than the script directory, you will need to omit the preceeding "./" eg. "getip" or if it gives you bad interpreter error use "perl getip" instead.

[cray@dyns]# ./getip

If you just want to check your current IP and not update DyNS, use the "checkip" parameter.

[cray@dyns]# ./getip checkip

If you sometimes edit the Host IP setting on DyNS directly from it's website, you'll find that your local IP and your Host IP may become out of sync. You will need to "force" update option and sync the two IPs by issuing the below command:

[cray@dyns]# ./getip force

Step 5

Lastly, you should want to run a cron task to execute getip once every half an hour or every one hour. To help you to create cron tasks easily, use kcron. Below is an example screenshot of a kcron task setting page. I have set it to run getip once every hour, everyday.

 

 

Important : Although getip only updates DyNS when it detects that the local IP has changed, everytime you run getip, it accesses the IP Address Checker and create traffics to the BNL server. So please do not run the cron job more often than necessary especially if your webservers or FTP servers are non-critical hobby servers.

If you are only interested in putting the program to work for you, you will probably stop reading here. But if you are interested in the working principles or would like to modify and enhance it further, we'll briefly look into the few main codes of the program in the next section.

 Note : for updates to programs please visit http://serve.me.nus.edu.sg/limcg/getip.html

A brief discussion on getip

I'll assume you have knowledge of perl and regular expressions before we start off. These are 2 really big topics that cannot be sufficiently discussed in a short article like this. Now, let us take a look at the main parts of the program. I will back-reference the below discussion to the earlier section, simple solution.

1. Query IP Address Checker for the latest external IP:

051 $new_ip=`lynx -dump "$ip_address_checker_url"`;
052 $new_ip=~/\s+Your IP Address is:\s+(\d+\.\d+\.\d+\.\d+)/;
053 $new_ip=$1;

I have assigned the variable $new_ip to the reply from querying IP Address Checker on line 51. In line 52, the dot formatted IP is extracted and automatically placed in perl's $1 positional parameter and is finally assigned to $new_ip on line 53. So after these statements, $new_ip contains the latest external IP address.

2.

  • If this is the first time you run getip,
    • write the latest external IP into a local compare file for future comparison
    • update DyNS with the external IP

102 if (!(-e $current_ip_file)) # running getip for the 1st time. compare file not found
103 {
104     &dyns_update;
105 }

Line 102 says that if the current IP file (our compare file) does not exist the program will run a sub-routine called &dyns_update.

63 sub dyns_update #sub-routine to update DyNS with the latest IP
64 {
65     $update_status=`lynx -dump "$dyns_update_url?username=$username&..."`;
66     if ($update_status=~/200/)
67     {
68         &store_current_ip;
69     }
70     elsif ($update_status=~/wait +(\d+) +s/)
71     {
72         print "Updating too frequently. Please try again $1s later.\n";
73     }
74     else
75     {
76         print "Update is unsuccessful. Please try again later.\n";
77     }
78 }

The first thing &dyns_update does is to initiate a DNS update request to DyNS, again with the help of lynx (line 65). Do read up on the DyNS update protocol here. If the update is successful, DyNS will reply with a status number "200" then another sub-routine, &store_current_ip, will be executed. This sub-routine simply prints the IP information to a file for future comparison purposes. Line 70 checks for another common DyNS reply: updating too frequently (note that updates to DyNS must at least be 5 minutes apart) and line 76 is executed for any other update status replies.

3.

  • Else
    • if the latest external IP is the same as that stored in the local compare file
      • do not update DyNS (since IP has not changed), exit
  • else
    • write the latest external IP into the local compare file for future comparison
    • update DyNS with the external IP

     

    106 else
    107 {
    108     open INFILE, "$current_ip_file";
    109     $current_ip=<INFILE>;
    110     close INFILE;
    111     if ($current_ip eq $new_ip)
    112     {
    113         print "There is no change in IP. No update to Dyns required\n";
    114     }
    115     else
    116     {
    117         &dyns_update;
    118     }
    119 }

    Lines 109 -110 opens the compare file created earlier for reading the current IP. This IP is compared with $new_ip on line 111. If it hasn't change, no update is required and the program exits. If there are indeed changes in the IP, the sub-route &dyns_update is called again and the update sequence is performed once again.

The above are brief descriptions of the program mains. I have added other simple command-line parameter-based functionalities. Look throught getip script if you are interested.

 

Conclusion

I hope this simple little program will be able to help you in setting up Dynamic DNS for your Fedora, or for that matter, any Linux boxes in straight PC-to-modem connections or behind router connections. It might even be useful for users who are on analog modem dialup connections but need to be easily identified for the duration of that connection.

I am no expert in Linux systems and programming languages but I am very interested tolearn and share more about it. My wholehearted thanks goes to the Fedora team for their selfless works in bringing to us Fedora Linux and also to the Fedoranews team for caring and sharing and taking the initiative to setup such a wonderful knowledge-based system for us all to exchange ideas and learn.

Lastly, If you have any comments for me, you can email me at here or here. Thank you for your time!

Note : for updates to programs please visit http://serve.me.nus.edu.sg/limcg/getip.html