Shorewall DDNS script

Since running Shorewall on my APU2c4 running a EL distro, I found myself replicating all of the built-ins that were present in other all-in-one projects such as pfSense or openWRT. Here’s one of my quick scripts to replace some of that functionality. This checks and updates my domain’s DNS from my Shorewall router hourly.

It performs a check of the IP reported by my domain’s resolver against the IP of my default gateway, and updates using the DDNS URL if necessary.

Notes:
– Not tested against IPv6 or complex routing scenarios!
– Place in /etc/cron.d/hourly or /etc/cron.d/daily
– The DDNS_QUERY var is highly dependent on your DDNS provider, adjust as necessary


#!/bin/bash
# Simple Dynamic DNS Script
# - Use-case: UNIX-based server acting as front door router
# - Performs comparison between domain A versus default gw
# - Not tested with IPv6 or complex routing scenarios (dual WAN, etc.)

DOMAIN="YOUR_DOMAIN"
RESOLVER="YOUR_DOMAINS_DNS_RESOLVER" # dig $YOUR_DOMAIN ns

DNSIP=$(dig +short @${RESOLVER} ${DOMAIN}) # get IP address of domain from resolver
WANIP=$(ip route get 1 | awk '{print $NF;exit}') # get IP address of default gateway

DDNS_URL='YOUR_URL'
DDNS_PASS='YOUR_PASS'
DDNS_HOST='YOUR_HOST' # the host record (TLD, '@', etc)
DDNS_QUERY="update?host=${DDNS_HOST}&domain=${DOMAIN}&password=${DDNS_PASS}&ip=${WANIP}"

DNSIP=$(dig +short @${RESOLVER} ${DOMAIN})
WANIP=$(ip route get 1 | awk '{print $NF;exit}')

#echo ${DNSIP}
#echo ${WANIP}

if [ "${DNSIP}" != "${WANIP}" ]; then
curl --silent "${DDNS_URL}/${DDNS_QUERY}" 2>&1 1>/dev/null
fi

Comments are closed.