The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
Dynamic DNS - detecting and updating 18 July 1999
Need more help on this topic? Click here
This article has no comments
Show me similar articles
This article describes some scripts which allow my machine to detect a change in IP address and update the DNS tables automatically.

I thank the following people for their help in developing these scripts:

Harold Jarvie - for developing the original script.

Burke Gallagher - for converting the script to use sockets.

Jake Burkholder - for showing me how to send mail from within PERL.

19 August 1999 - scripts updated
The and scripts have each been updated.  My ISP has recently added a www proxy.  This caused problems with the script. was using the IP address of the proxy instead of my address.  That makes sense, however useless it may be.  However, the fix was quite simple.  I pass the IP address from the perl script to shell script and all works well.
The script has been updated.  Until today, it contained the original version which used lynx.  This version uses fetch.   If you downloaded the old version and couldn't get it to work, I apologise.   Please try this one.

I've also been given another script by Dragon Knight <>.  It also works with   Have a look at  Please note that the script runs under bash and produces an error if run under sh.

Dragon Knight <> has updated his script.  Have a look at the new
Dragon Knight <> has updated his script.  This change makes sure that your default route is cleared if, say, you unplug your modem.  Have a look at the new
Dynamic DNS?
It is quite common for people to have dynamic IP addresses.  Such addresses change from time to time.  These changes may be random or they may change each time you dial up your ISP.  There are also some cable and ADSL providers which supply connections via DHCP.  A dynamic IP address makes it awkward it not difficult to run any services such as mail, www, ftp, etc.  The problem is that you can't use these services without knowing the current IP address.

DNS (Domain Name Service) is the method by which IP addresses and domain names are associated.  If your IP address changes, then you need to update your DNS records to reflect this change.  And perhaps people will find another website instead of yours.

Choose a DYNDNS provider
Your first step is to select a provider of dynamic DNS services.  Typically, you will receive a domain name from them such as or   You can use this domain or you can use your own domain such as, in which case, the other domain can be an alias for your real domain.  I'll show you how to do this in a later section.

Several organizations exist which supply dynamic DNS services.  Here are the ones I know about.  If you know of any more, please give us your comments.

URL Site Title Quick, Easy, Painless Dynamic DNS Project Detour TJ Network Services Dynamic Host Information System DNS Wizard Corporation Easy DNS (not sure they provide Dynamic DNS) Dynamic DNS Network Services The TZO Internet Naming Utility ($24.95)

I use, but I'm sure you'll be able to substitute the script from your DYNDNS provider.  Some of the above are free, some of them cost.  You get what you pay for.

Test the interface
The provider will supply some method of updating the IP address.  This might be a www interface or a custom program which does the update for you.  You will need to download and possibly install whatever the provider supplies.

Make sure the the code from your DYNDNS provider actually updates their DNS for you.   This is the most important step.  If you have trouble here, please contact your DYNDNS provider.

Dial up connections
If you are on a dial up connection, this solution is simple.  Just call the provider's code each time you dial up.  Sometimes they provide another bit of code for hanging up.  Please follow their instructions.
Permanent connections
An ADSL or cable connection requires a more complex solution than a dial up.  With a permanent connection you must first detect that your IP address has changed.  I've taken the approach that I check my IP every 10 minutes and if it has changed from the last check, then I update my DNS.
Advanced DHCP Solutions
I have been told, but I have not investigated the following.  If you are running a DHCP client on your box, and chances are that you will be, there is a way to invoke a script when the IP address changes.  If you are running the ISC-DHCP client, there is a shell script that does all the OS dependent interface manipulations ("dhclient-script").  I'm told that modifying this script to run something after being called for a reason of "BOUND" is a solution for DYNDNS.
My solution
The solution involves four files, but only two contain scripts.  The other two are used for storage.  I have supplied HTML versions of the the following files but you can download them from the following links.

a perl script run every ten minutes from /etc/crontab

myip.txt stores the last known IP address
fetch_results.html holds the result of the last attempt to update our DNS

a shell script provided by but modified for my purposes.  runs when a change in IP address is detected. (html version)

I have an ADSL modem/router.  It is this router which actually receives the new IP address.  My FreeBSD box retains the same address all the time, but it is a private address and is not accessible via the Internet (i.g. it's a 192..168.*.* address).  The router has a www interface, so I can browse to it and see what the IP address is for it.   This is pretty simple.  I've provided a screen shot of this interface.  The IP address we need to find is in bold.  It is the FetchM10Address routine in which parses this interface and extracts the current IP.

I run the perl script.  Here is the entry in /etc/crontab which I use to start my script every ten minutes:

*/10 * * * * dan /home/dan/ > /dev/null

One thing to note is that I am not using the default solution.  I had problems running lynx from a cron job.  So I modified the script to use fetch.

How it works
This is a brief description of how these scripts and cron jobs work together.
  1. Every ten minutes, the cron job starts up /usr/home/dan/
  2. This code queries the ADSL router for the current IP address.
  3. The current IP address is compared against the last IP address which was stored in /usr/home/dan/myip.txt.
  4. If the IP address has changed, the script from the DYNDNS provider (/usr/home/dan/ is invoked.
  5. The results of that dns update are stored in /user/home/dan/fetch_results.html.
Testing and what you should expect
If you are using the above scripts, you'll have to modify them for your own needs.   The HTML version displays the required modifications in bold.  After that, you should be able test the script.  Here's what it should look like the first time you run it:
# perl 
currentIP =,  lastIP = 
change detected
Updating to IP
count = 2

Result = '1'
update succeeded

Here are the log entries it generated:

Jul 18 17:29:50 ns DYDNS[23394]: start
Jul 18 17:29:55 ns DYDNS[23401]: The IP Address has changed to 
Jul 18 17:29:56 ns DYDNS[23405]: stopped

And here's the email message you should get.  Note that the message contains HTML because that's what the script fetches.  It is a www interface.

$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/mail/dan": 1 message 1 new
>N  1 dan              Sun Jul 18 17:30  24/930   "Dynamic DNS change"
& 1
Message 1:
From dan Sun Jul 18 17:30:04 1999
Date: Sun, 18 Jul 1999 17:29:58 +1200 (NZST)
From: Dan <dan>
To: dan, root
Subject: Dynamic DNS change

The IP Address has changed to
<HEAD><TITLE> Domain Update Request</TITLE></HEAD>
<BODY>This update will only service IP changes. To reconfigure your settings, please use the account editor available at
<A HREF=""></A>.<BR>
<!-- MS3V STATUS:OK HOSTNAME:yourdomain ACT:1 IP: MX:na 
Your request has been processed successfully.
<UL><LI><B>Domain Name:</B> yourdomain</LI>
<LI><B>IP Address:</B></LI></UL>
<EM>Report errors to <A HREF=""></A></EM><BR>

The next time you rung the perl script, you should see this:

# perl 
currentIP =,  lastIP =

Here are the log entries that will generate:

Jul 18 17:36:17 ns DYDNS[23425]: start
Jul 18 17:36:18 ns DYDNS[23426]: stopped
What this gives you
Assuming you have chosen as your DYNDNS provider, this will enable anyone to find your box by using  But perhaps you have another domain, such as  This solution doesn't work for that.  But there is a way.   It involves using a third party to provide DNS servers to you, but it does work and it works rather well.  See the next section for more information.
A domain alias is easy to set up.  It is most often done with a CNAME.  Here is a well known ftp site which is actually just an alias:
$ nslookup

Non-authoritative answer:

A domain alias is easy to set up.  It is most often done with a CNAME.  For example, here's what I use as the zone file for

@       IN      SOA  (
                                199907031       ; Serial
                                3600    ; Refresh
                                900     ; Retry
                                3600000 ; Expire
                                3600 )  ; Minimum

; name servers

                IN      NS
                IN      NS
                IN      MX      5
; Define the rest of my subnet
;            IN CNAME            IN CNAME

With this setup, people can get to my box at home using  This will have the same IP address as  To learn how different websites can share the same IP addresss, see Virtual Hosts.

Where to from here
This is not a simple process. There are many steps.  The scripts may not work for you.  They may need modification.  Go slowly, one step at a time.  Get the DYNDNS working first, then worry about the CNAME type of thing.

Best wishes.  And I hope this helps.

dhclient hooks 20 May 2000
Chris Wicklein write in to say the following URL has notes on using dhclient's exit hooks: section 4.

Need more help on this topic? Click here
This article has no comments
Show me similar articles