Duck DNS

Duck DNS

Set up a dynamic DNS cron job using domain name and access token from Duck DNS.

Script

config-duckdns.sh

#!/bin/bash

usage() {

  if [[ -n "$2" ]]; then
    echo "$2" 1>&2
  fi

  echo "Usage: $0 -d domain -t token" 1>&2
  exit $1

}

while getopts "hd:t:" current; do
  case "${current}" in
    d)
      DOMAIN="${OPTARG}"
      ;;
    t)
      TOKEN="${OPTARG}"
      ;;
    h)
      usage 0
      ;;
    :)
      usage 3 "Missing value for -${OPTARG}"
      ;;
    *)
      usage 4
      ;;
  esac
done

if [[ -z "${DOMAIN}" ]]; then
  usage 1 "duckdns.org domain name required"
fi

if [[ -z "${TOKEN}" ]]; then
  usage 2 "duckdns.org token required"
fi


mkdir -p ~/duckdns
cd ~/duckdns

cat <<EOF > duck.sh
echo url="https://www.duckdns.org/update?domains=${DOMAIN}&token=${TOKEN}&ip=" | curl -k -o ~/duckdns/duck.log -K -
EOF

chmod 700 duck.sh

if [[ $(crontab  -l) = "no crontab for ${USER}" ]]; then
  echo "*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1" | crontab -
else
  ( crontab -l ; echo "*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1" ) | crontab -
fi

Usage

./config-duckdns.sh -d DOMAIN -t TOKEN

Additional Information

  1. Create an account at http://www.duckdns.org if you do not already have one.

  2. Follow the instructions there to create the domain you want to use for your Pi if you have not already done so.

  3. Use the FQDN (e.g. mypi.duckdns.org) and access token for the domain you created in step 2 when invoking config-duckdns.sh.

In order for the configuration performed by config-duckdns.sh to be effective, you must at minimum also configure your router to forward whatever ports you want exposed to the Internet to your Pi and set up the corresponding services such as Apache, Mosquitto and so on.