Dynamic DNS – detecting and updatingThis 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 updatedThe dns_fetch.sh and obtain_ip.pl scripts have each been updated. My ISP has recently added a www proxy. This caused problems with the yi.org script. yi.org 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. dns_fetch.sh 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 <firstname.lastname@example.org>. It also works with yi.org. Have a look at dragon.yi.org.sh. Please note that the script runs under bash and produces an error if run under email@example.com> has updated his script. Have a look at the new dragon.yi.org.sh. firstname.lastname@example.org> 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 dragon.yi.org.sh.
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 providerYour first step is to select a provider of dynamic DNS services. Typically, you will receive a domain name from them such as yourdomain.yi.org or yourdomain.dhis.org. You can use this domain or you can use your own domain such as yourdomain.org, 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 http://www.yi.org/ Quick, Easy, Painless Dynamic DNS http://detour.net/ Project Detour http://tjns.tj/ TJ Network Services http://www.dhis.org/ Dynamic Host Information System http://www.dnswiz.com/ DNS Wizard Corporation http://www.easydns.com/ Easy DNS (not sure they provide Dynamic DNS) http://www.dyndns.org/ Dynamic DNS Network Services http://www.tzo.com The TZO Internet Naming Utility ($24.95)
I use yi.org, 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 interfaceThe 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 connectionsIf 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 connectionsAn 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 SolutionsI 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 solutionThe 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 yi.org 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 obtain_ip.pl 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/obtain_ip.pl > /dev/null
One thing to note is that I am not using the default yi.org solution. I had problems running lynx from a cron job. So I modified the script to use fetch.
How it worksThis is a brief description of how these scripts and cron jobs work together.
- Every ten minutes, the cron job starts up /usr/home/dan/obtain_ip.pl.
- This code queries the ADSL router for the current IP address.
- The current IP address is compared against the last IP address which was stored in /usr/home/dan/myip.txt.
- If the IP address has changed, the script from the DYNDNS provider (/usr/home/dan/dns_fetch.sh) is invoked.
- The results of that dns update are stored in /user/home/dan/fetch_results.html.
Testing and what you should expectIf 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 obtain_ip.pl DYNDNS currentIP = 220.127.116.11, lastIP = change detected Updating to IP 18.104.22.168 count = 2 Result = '1' update succeeded
Here are the log entries it generated:
Jul 18 17:29:50 ns DYDNS: start Jul 18 17:29:55 ns DYDNS: The IP Address has changed to 22.214.171.124 Jul 18 17:29:56 ns DYDNS: stopped
And here’s the email message you should get. Note that the message contains HTML because that’s what the yi.org 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 126.96.36.199 <HTML> <HEAD><TITLE>yi.org Domain Update Request</TITLE></HEAD> <BODY>This update will only service IP changes. To reconfigure your yi.org settings, please use the account editor available at <A HREF="http://www.yi.org/">www.yi.org</A>.<BR> <!-- MS3V STATUS:OK HOSTNAME:yourdomain ACT:1 IP:188.8.131.52 MX:na --> Your request has been processed successfully. <UL><LI><B>Domain Name:</B> yourdomain</LI> <LI><B>IP Address:</B> 184.108.40.206</LI></UL> <EM>Report errors to <A HREF="mailto:email@example.com">firstname.lastname@example.org</A></EM><BR> </BODY></HTML>
The next time you rung the perl script, you should see this:
# perl obtain_ip.pl DYNDNS currentIP = 220.127.116.11, lastIP = 18.104.22.168
Here are the log entries that will generate:
Jul 18 17:36:17 ns DYDNS: start Jul 18 17:36:18 ns DYDNS: stopped
What this gives youAssuming you have chosen yi.org as your DYNDNS provider, this will enable anyone to find your box by using yourdomain.yi.org. But perhaps you have another domain, such as yourdomain.org. 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.
$ nslookup ftp.freebsd.org Server: localhost.yourdomain.org Address: 127.0.0.1 Non-authoritative answer: Name: wcarchive.cdrom.com Address: 22.214.171.124 Aliases: ftp.freebsd.org
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 racingsystem.cx:
@ IN SOA rock.ghis.net. soa.dvl-software.com. ( 199907031 ; Serial 3600 ; Refresh 900 ; Retry 3600000 ; Expire 3600 ) ; Minimum ; name servers IN NS ns.ghis.net. IN NS ns.freebsddiary.cx. IN MX 5 freebsddiary.yi.org. ; ; Define the rest of my subnet ; www.freebsddiary.cx. IN CNAME freebsddiary.yi.org. ww3.racingsystem.cx. IN CNAME freebsddiary.yi.org.
With this setup, people can get to my box at home using www.freebsddiary.cx. This will have the same IP address as freebsddiary.yi.org. To learn how different websites can share the same IP addresss, see Virtual Hosts.
Where to from hereThis 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.