Skip to content

Instantly share code, notes, and snippets.

@caleblloyd
Last active May 1, 2019 00:22
Show Gist options
  • Save caleblloyd/43f468e547ada577bb6aa705bc1c82ff to your computer and use it in GitHub Desktop.
Save caleblloyd/43f468e547ada577bb6aa705bc1c82ff to your computer and use it in GitHub Desktop.
Dreamhost API Dynamic DNS cron script
  1. sign up for a dreamhost API Key with "All DNS" permissions at https://panel.dreamhost.com/index.cgi?tree=home.api
  2. copy config.sh to /etc/dreamhost/config.sh and fill out the variables
  3. copy ddns.sh to /usr/local/dreamhost/ddns.sh
  4. copy dreamhost_cron to /etc/cron.d/dreamhost_cron
#!/bin/sh
# file location: /etc/dreamhost/config.sh
KEY="AAA1112223334444" # dreamhost api key
DDNS_SUBDOMAIN="subdomain.example.com" # dynamic dns subdomain
DDNS_INTERFACE="eth0" # interface to use IPV4 from
DDNS_LAST_IP_FILE="/var/lib/dreamhost/last_ip.txt" # file to store most recent IPV4 in
#!/bin/sh
# file location /usr/local/dreamhost/ddns.sh
source /etc/dreamhost/config.sh
last_ip=""
if [ -f $DDNS_LAST_IP_FILE ]
then
last_ip=$( cat $DDNS_LAST_IP_FILE | head -n 1 | awk '{ print $1}' )
fi
ip=$( ifconfig $DDNS_INTERFACE | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' )
if [ "$ip" != "$last_ip" ]
then
echo "Updating IP Address to $ip"
dh_last_ip=$(curl -s -H "Content-Type: application/x-www-form-urlencoded" -X POST -d \
"key=$KEY&cmd=dns-list_records" \
https://api.dreamhost.com/ \
| grep $DDNS_SUBDOMAIN | cut -d: -f2 | awk '{print $5}')
curl -s -H "Content-Type: application/x-www-form-urlencoded" -X POST -d \
"key=$KEY&cmd=dns-remove_record&record=$DDNS_SUBDOMAIN&type=A&value=$dh_last_ip" \
https://api.dreamhost.com/
curl -s -H "Content-Type: application/x-www-form-urlencoded" -X POST -d \
"key=$KEY&cmd=dns-add_record&record=$DDNS_SUBDOMAIN&type=A&value=$ip" \
https://api.dreamhost.com/
mkdir -p $( dirname $DDNS_LAST_IP_FILE )
echo "$ip" > $DDNS_LAST_IP_FILE
fi
# file location /etc/cron.d/dreamhost_cron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root /bin/bash /usr/local/dreamhost/ddns.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment