Skip to content

Instantly share code, notes, and snippets.

@voxxit
Last active July 14, 2019 12:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save voxxit/95282598a5c4277f368b to your computer and use it in GitHub Desktop.
Save voxxit/95282598a5c4277f368b to your computer and use it in GitHub Desktop.
Dynamic DNS script for DigitalOcean. For example: to use with OpenWRT routers, install bash (opkg install bash) then place this script in /etc/hotplug.d/iface/
#!/usr/bin/env bash
set -x
# Domain you wish to update
DOMAIN="example.com"
# Get the v4/v6 addresses from icanhazip.com [pretty reliable!]
IPV4_ADDR=`wget -4 -q -O - icanhazip.com`
IPV6_ADDR=`wget -6 -q -O - icanhazip.com`
# Get your v2 API token from here: https://cloud.digitalocean.com/settings/tokens/new
TOKEN="..."
# Add the record IDs you wish to update here
V4_RECORDS=(123 ...)
V6_RECORDS=(678 ...)
for RECORD in $V4_RECORDS
do
wget \
-O - \
--quiet \
--no-check-certificate \
--method="PUT" \
--header="Content-Type: application/json" \
--header="Authorization: Bearer $TOKEN" \
--body-data="{\"data\":\"$IPV4_ADDR\"}" "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD"
done
for RECORD in $V6_RECORDS
do
wget \
-O - \
--quiet \
--no-check-certificate \
--method="PUT" \
--header="Content-Type: application/json" \
--header="Authorization: Bearer $TOKEN" \
--body-data="{\"data\":\"$IPV6_ADDR\"}" "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD"
done
@yurymik
Copy link

yurymik commented Jun 6, 2018

Really nice script, it helped me to bootstrap mine 😄

A few suggestions if I may:

Lines 8-9:

  • instead of passing -4 and -6 to wget you can query explicitly ipv4.icanhazip.com and ipv6.icanhazip.com respectively.
  • use https:// for extra bit of security

Lines 14-16:

  • please add an explanation where to get those ids. GET on https://api.digitalocean.com/v2/domains/$DOMAIN_NAME/records should return them.

Lines 23 and 35:

  • why --no-check-certificate? DigitalOcean returns valid certificate and by skipping the check you're downgrading security of your connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment