Skip to content

Instantly share code, notes, and snippets.

@benkay86
Created April 23, 2020 18:04
Show Gist options
  • Save benkay86/972a8ce6623d5e24f77465ca3d4306aa to your computer and use it in GitHub Desktop.
Save benkay86/972a8ce6623d5e24f77465ca3d4306aa to your computer and use it in GitHub Desktop.
Simple dynamic DNS script for OpenWRT using Dreamhost API
#!/bin/sh
# Simplified script for using Dreamhost as a dynamic DNS provider.
# Works on openwrt, but must have ssl support.
# You will probably need to install libustream-openssl and ca-certificates.
# You may need to edit IP detection lines for your specific configuration.
# You could use cron to schedule it to run hourly:
# crontab -e
# 0 * * * * /root/dreamhost.sh
# Don't forget to enable cron!
# /etc/init.d/cron enable
# /etc/init.d/cron start
## Beginning of Configuration ##
# Dreamhost API key.
# Must have permission for dns-list_recort, dns-remove_record, and dns-add_record.
KEY=XXXXXXXXXXXXXXXX
# Record you want to update.
RECORD="hostname.mydomain.com"
# WAN interface from which to obtain public IP addresses.
IFACE="eth0.2"
## End of Configuration ##
# Function for updating Dreamhost record with new IP address $ADDR.
update_record()
{
# Type of record, A for IPV4 or AAAA for IPV6.
TYPE=$1
# Query Dreamhost to see if there is an existing record.
unset NO_CREATE
unset OLD_ADDR
OLD_ADDR=`wget -O- -q https://api.dreamhost.com/ --post-data key=$KEY\&unique_id=$(uuidgen)\&cmd=dns-list_records\&type=$TYPE\&editable=1 | egrep "${RECORD}\s+${TYPE}\s+" | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-fA-F]{0,4}:){1,7}([0-9a-fA-F]){0,4}" | awk '{print toupper($0)}'`
if [ -n "$OLD_ADDR" ]
then
# Is our corrent IP address different from the existing record?
if [ "${ADDR}" = "${OLD_ADDR}" ]
then
# Do not update the record.
echo "Type $TYPE record for $ADDR is already up to date."
NO_CREATE=1
else
# There is no "update" API.
# Must remove old record before creating new one.
echo "Removing old type $TYPE record for $OLD_ADDR"
wget -O- -q https://api.dreamhost.com/ --post-data key=$KEY\&unique_id=$(uuidgen)\&cmd=dns-remove_record\&record=$RECORD\&type=$TYPE\&value=$OLD_ADDR
fi
fi
if [ -z "$NO_CREATE" ]
then
# Create a new record unless the old record was already up to date.
echo "Creating new type $TYPE record for $ADDR"
wget -O- -q https://api.dreamhost.com/ --post-data key=$KEY\&unique_id=$(uuidgen)\&cmd=dns-add_record\&record=$RECORD\&type=$TYPE\&value=$ADDR
fi
}
# Look up public IP4 address and update A record.
ADDR=`ip -4 addr show dev $IFACE | egrep inet | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -n 1`
update_record A
# Look up public IP6 address and update AAAA record.
# Note that this address might not match the old record exactly because one of
# Dreamhost or ip might use a compressed address format while the other uses
# an expanded format. In the worst case this will lead to an unneeded address
# update, which is honestly not that big of a deal.
ADDR=`ip -6 addr show dev $IFACE | egrep "inet6.+global" | egrep -o "([0-9a-fA-F]{0,4}:){1,7}([0-9a-fA-F]){0,4}" | awk '{print toupper($0)}'`
update_record AAAA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment