My Little Corner of the Net

Dead Simple Dynamic DNS Updater

I run a VPN on my home network which lets me access my systems and files remotely and gives me a secure route to the Internet when I have to use questionable networks. Since my Internet provider does not give me a static IP address, I rely on dynamic DNS services to keep my IP mapped to a hostname I can always use to “phone home.”

Since the DNS servers for the service I’ve been using seemed to vanish a couple weeks ago, I started “shopping” for a new provider and came across dtDNS. dtDNS allows you to set up five dynamic DNS hostnames for free, or you can pay a $5.00 one-time fee to get unlimited (“within reason,” according to the site) hosts.

Once I had my new hostname set up, it was time to set up a client app to keep my IP in sync. I had some trouble getting ddclient, which I’ve been using for a while now, to work with dtDNS, and the Linux options on dtDNS’s update clients page were either no longer available, required Java, or expected the machine to have a public IP address, which mine does not. So with a bit of research, I wrote my own.

My updater is a simple shell script with less than 10 lines of code. It uses icanhazip.com to find the external IP address, so it will work on systems that don’t have public IPs, and it only pushes a change request when it sees that the IP has changed.

#!/bin/bash
# dtDNS Dynamic IP update Script
# Author: Jason R. Pitoniak 
# 
# Copyright (c) 2015 Jason R. Pitoniak

# Set your dtDNS hostname and password below
HOSTNAME='MYNAME.dtdns.net'
PASSWORD='PASSWORD'

# We need to find your external IP address as your system may have an non-public address
# on your local network. icanhazip.com (or any number of other sites) will do this for us
EXTIP=`curl -s http://icanhazip.com/`

# Now we check which IP dtDNS currently has recorded by checking their DNS server
LASTIP=`nslookup $HOSTNAME ns1.darktech.org | tail -2 | awk '{ print $2 }'`

# If the current external IP is different from the one with dtDNS, update dtDNS
if [ "$EXTIP" != "$LASTIP" ]
then
    curl "https://www.dtdns.com/api/autodns.cfm?id=$HOSTNAME&pw=$PASSWORD&ip=$EXTIP"
fi

It should run on any Unix-like system including Mac OS X. It will probably even work on Windows with cygwin, but I haven’t tried. Just copy it to a file named dtdns-update somewhere on your system, update the HOSTNAME and PASSWORD variables to reflect your account, and chmod the file so that it is accessible only to the user that will run it:

chmod 700 dtdns-update

To test the script, call it from the command line:

/path/to/dtdns-update

The script will return whatever response it receives from the dtDNS update API, whether it is an error or success message. If nothing is returned it means that dtDNS already has the correct IP, so no action was taken.

Now we’ll set up a cron job to run the script periodically. To do this, enter the following on the command line:

crontab -e

A text editor will open. Add the following to the end of the file:

*/5 * * * * /path/to/dtdns-update >/dev/null 2>&1

This will run the script once every five minutes. You can adjust the interval as you feel is appropriate. Once you save the file, the new cron job will be installed and will begin running within a few minutes. Now you can rest assured that your IP address will always be up to date with dtDNS.

Protip: If your dtDNS hostname is too difficult to easily remember and you own a domain name, you can set up a hostname on your own domain that points to your dtDNS name. If you maintain your own DNS, create a CNAME record for whatever host name you want with your dtDNS hostname as the target. If you don’t maintain your DNS yourself, ask you host if they can configure this for you.

8 Comments to Dead Simple Dynamic DNS Updater

  1. StSchHN's Gravatar StSchHN
    08/19/2015 at 1:16 am | Permalink

    Using the script for a few days now I have realized that LASTIP is sometimes empty, which probably means that nslookup didn’t return a result. Maybe increasing the timeout value (nslookup -timeout=30) solves this problem.

  2. 08/07/2016 at 8:31 am | Permalink

    Hi,
    I have slightly modified your script by using dig instead of nslookup:

    LASTIP=`dig +short ${HOSTNAME}`

  3. 11/18/2016 at 4:01 am | Permalink

    Hello,
    Thanks for this small but useful script…
    Philippe

  4. Steve Harman's Gravatar Steve Harman
    11/18/2016 at 4:21 am | Permalink

    Hi,
    Should the script return to the command prompt after execution? It seems to be working fine in terms of updating dtdns, I just never end up back at a prompt after running it.
    Thanks,
    Steve

  5. Zozi's Gravatar Zozi
    04/03/2017 at 4:53 am | Permalink

    Thank you! 😉

  6. François's Gravatar François
    05/29/2017 at 5:21 am | Permalink

    Script just refused to do anything until I ran it in a terminal and saw :

    /usr/bin/dtdns-update: ligne 13: curl : commande introuvable

    Lubuntu 16 does not come with curl installed as standard. Once I installed curl, everything went like a dream. Went into dtdns control panel and entered a fictitious IP address. Ran the script and it updated to the current IP instantly.

    Thank you Kodiak for a great little script that does what it says on the tin.

    Thank you Francesco Illuminati for your “LASTIP” tip.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

<