Skip to content

Instantly share code, notes, and snippets.

@joemiller
Last active September 4, 2024 18:46
Show Gist options
  • Save joemiller/9fcbf1c953a8ed1095e95fe4396cec4a to your computer and use it in GitHub Desktop.
Save joemiller/9fcbf1c953a8ed1095e95fe4396cec4a to your computer and use it in GitHub Desktop.
script for updating dynamic DNS records on he.net (hurricane electric)
#!/bin/bash
#
# Script for updating DNS records on Hurricane Electirc's DNS system (https://dns.he.net).
#
# The record will be updated with the IP address that originates the request.
#
# Usage
# -----
#
# Create config file `/etc/he-dns-update.conf`:
#
# HOSTNAME=myhost.dom.tld
# KEY=<update_key>
#
# Update IPv4 (A) record:
#
# $ ./he-dns-update.sh 4
#
# Update IPv6 (AAAA) record:
#
# $ ./he-dns-update.sh 6
#
# Logs / Output
# -------------
#
# Output is sent to syslog instead of stdout (unless there is an error with the config file).
#
# Examples:
#
# Apr 1 12:00:03 gw HE-DNS: OK: ipv4 address updated: good 12.34.56.78
# Apr 1 12:00:05 gw HE-DNS: OK: ipv4 address no change: nochg 12.34.56.78
# Apr 1 12:00:11 gw HE-DNS: ERROR: ipv6 address not updated : curl timed out.
#
# Cron example
# ------------
# Hourly:
#
# 0 * * * * /usr/local/bin/he-dns-update.sh 4
# 0 * * * * /usr/local/bin/he-dns-update.sh 6
#
# Author
# ------
# Joe Miller / https://github.com/joemiller 2016
#
LOGGER_BIN="logger"
CURL_BIN="curl"
CURL_TIMEOUT_SECS=10
# load config file and verify required args
. "/etc/he-dns-update.conf"
if [ -z "$HOSTNAME" ] || [ -z "$KEY" ]; then
echo "ERROR: Config file '/etc/he-dns-update.conf' is missing one or more required vars: HOSTNAME, KEY"
exit 1
fi
proto="$1"
if [ -z "$proto" ]; then
echo "Usage: $0 [4|6]"
exit 1
fi
out=$("$CURL_BIN" \
-"$proto" \
--silent \
-m "$CURL_TIMEOUT_SECS" \
-k \
https://dyn.dns.he.net/nic/update \
-d "hostname=$HOSTNAME" \
-d "password=$KEY" 2>&1)
# $out will be empty on timeout, so we catch curl's return code for timeout and populate
# $out with a helpful message. Could do this for other common errors from curl too.
rc=$?
if [ "$rc" == 28 ]; then
out="curl timed out."
fi
# possible outputs from curl:
#
# on success, either:
# good 192.168.0.1
# nochg 192.168.0.1
#
# on error:
# could be anything else. we log an error with the full output.
case $out in
*good*)
"$LOGGER_BIN" -p local0.info -t HE-DNS "OK: ipv$proto address updated: $out" ;;
*nochg*)
"$LOGGER_BIN" -p local0.notice -t HE-DNS "OK: ipv$proto address no change: $out" ;;
*)
"$LOGGER_BIN" -p local0.err -t HE-DNS "ERROR: ipv$proto address not updated : $out" ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment