Script for automatic duckdns updates

Posting this, hoping it helps someone someday!

I wanted to be able to access a relative's network periodically to help with network maintenance. I had provided them an OpenWRT router all set up, ready to go - with a few additional mods. I set up a https://duckdns.org subdomain for free, and made a quick script to keep it updated. I also added my ssh key and disabled password access. Moments after they plugged it in, I could access it!

Here's the simple script I made:

Create /etc/config/duckdns.sh: (replace DUCKDNS info in the curl command)

#!/bin/sh

#let the system boot up before attempting to check the ip address...
sleep 30

#install curl dependency (in case of import after firmware update)
opkg update 2>/dev/null 1>&2
opkg install curl 2>/dev/null 1>&2

OLDIP=''

while :
do
#capture interface addresses
  IP=$(ip addr | grep 'inet')  

  if [ "$IP" != "$OLDIP" ]; then
    #change detected! Update duckdns.org
    OLDIP="$IP"
    curl "https://www.duckdns.org/update?domains=<DUCKDNS SUBDOMAIN>&token=<DUCKDNS TOKEN>&ip=" 2>/dev/null 1>&2
  fi

#Repeat the check after 2 minutes
sleep 120
done

Then add this to /etc/rc.local:

#Script for updating duckdns.org subdomain
/etc/config/duckdns.sh &

This is dirt simple and has been working for months. I am ignoring the OK/KO return from curl, because frankly, if its not 'OK', I can't do anything remotely anyhow...

The beauty of this method is that it never has to reach out to the internet unless a change has occurred.

duckdns is supported by ddns.

4 Likes