Updating Dynamic DNS accounts using a script on Linux

Introduction

One of the requirements for having a domain name (typically) is to have a static IP, one that does not change frequently. Ordinary people face a problem, most (if not all) residential Internet services have dynamic IP addresses which change once a day (like what I have).
Companies like dyndns.com, changeip.com, no-ip.com, to name a few, have a service that you can use to have a domain name, either dedicated or shared, which can accept this kind of frequent change. All what is needed is either a router that support DDNS, or an application that is installed on a PC.

A Linux Script

Most, if not all, DDNS companies use APIs for their service, thus this article is about API and scripting.
Steps:

  • Create a directory in /etc, let’s say cron.2min (2min refer to the cron timing):
  • cd /etc
    mkdir cron.2min
    chmod 755 cron.2min
  • Add a new line in /etc/crontab:
  • 0-59/2 * * * * root run-parts /etc/cron.2min

    instructing the crontab to run anything in /etc/cron.2min every 2 minutes continuously everyday.

  • Created a file called externalip.txt in /root directory and insert dummy data in it:
  • touch /root/externalip.txt
    echo "2" > /root/externalip.txt
    chmod 644 /root/externalip.txt

    This file will be used to store the discovered external IP address by the next script below.

  • In /etc/cron.2min, created a file called dynip.sh, with the contents as follows:
  • a=`cat /root/externalip.txt`
    b=`wget -q -O - http://ip.changeip.com:8245 | cut -f 2 -d "=" | cut -f 1 -d "-" -s | grep -m 1 ^`
    if [ $a != $b ]
    then
    # dyndns
    wget --delete-after https://user:pass@members.dyndns.org/nic/update?hostname=yourhost >/dev/null 2>&1
    # no-ip
    wget --delete-after https://user:pass@dynupdate.no-ip.com/nic/update?hostname=yourhost >/dev/null 2>&1
    # changeip
    wget --delete-after https://nic.changeip.com/nic/update?hostname=*1&u=user&p=pass >/dev/null 2>&1
    # opendns
    wget --delete-after https://user:pass@updates.opendns.com/nic/update >/dev/null 2>&1
    # update externalip.txt file
    echo $b > /root/externalip.txt
    fi
  • Issue the following command:
  • chmod 755 /etc/cron.2min/dynip.sh

    To make it executable.

What is Done

The script does the following:

  1. grab the content of externalip.txt,
  2. get the output of the link ip.changeip.com and extract the IP address only,
  3. compare them, if not equal, do the updates, and then put the new IP address in externalip.txt
  4. if equal, just quit