Dynamic DNS via shell script and IBM NS1 (nsone)

I am using IBM NSOne for my domain’s DNS. I went with them after trying some other providers. I really like the API driven nature of it and that it will be free for my usage. I wrote some management scripts available here.

With that script, I can now manipulate the record for my domain’s IP address when it changes, like so:

#!/bin/bash

NEWIP=`echo $1 | sed -e "s/ip=//g"`

DOMAINS='mydomain1.com mydomain2.com'

if [ "$NEWIP" != "" ]; then
  for DOMAIN in $DOMAINS
  do
    ./updatensonerecord $DOMAIN $DOMAIN A $NEWIP
  done
else
  echo "No IP passed"
fi

Usage is then like this:

./updatedynamicip <new IP>

If you have more complicated DNS zones that you are wanting to update you may consider using CNAMEs and/or linked records to be able to keep the updating side simple.

Updating via the UDM Pro

That’s all great to be able to update from a command line but we want this to be automatic and seamless.

I decided to just use a very simple CGI-bin handler via nginx on a VM within my home network. However, given how the UDM Pro works it needs to support SSL. You will need to install the fcgiwrap package first of all.

CGI-bin config:

server {
  listen 443 ssl http2;

  server_name <your hostname>;

  ssl_certificate <your ssl cert>;
  ssl_certificate_key <your ssl key>;
  include ssl_params;

  root /var/www/html;

  index index.html index.htm index.nginx-debian.html;

  location ~ ^/cgi {
    rewrite ^/cgi/(.*) /$1 break;

    include fastcgi_params;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    fastcgi_param SCRIPT_FILENAME /root/bin/simplecgi;
  }
}

Once you enable that script you should be able to run a simple script, in this case I have one at /root/bin/simplecgi on this particular system. That script would then be the one to run your DNS updater script and respond appropriately. The script I have for that is:

#!/bin/bash

PROGRAM=/root/dns/updatensone

if [ -x $PROGRAM ]; then
  echo "HTTP/1.0 200 OK"

  OUTPUT=`$PROGRAM $QUERY_STRING`
  LENGTH=$((${#OUTPUT} + 4))

  echo $OUTPUT >>/tmp/simplecgi.log

  echo "Content-Length: $LENGTH"
  echo ""
  echo "OK"
  echo ""
  echo $OUTPUT
else
  echo "HTTP/1.0 404 NotFound"
  echo "Content-Length: 9"
  echo ""
  echo "Not found"
fi

All that script does is to pass the querystring to a predetermined script (in this case our DNS updater) and then output an HTTP/1.0 compatible response, along with the output from the script it ran.

Then from the UDM Pro, just set up a ‘custom’ DNS handler that has the URL of the form:

<your host machine IP>/cgi-bin/?ip=%i

Then when your network updates its IP address, your domains will follow suit! Note: do not prepend the protocol, i.e, https:// to that URL.

If you want to test it immediately, SSH to your UDM Pro.

ps aux | grep inadyn

Then run the following using the configuration file you saw in the output from above. e.g.

inadyn -n -1 --force -f /run/ddns-eth8-inadyn.conf