Skip to content

Instantly share code, notes, and snippets.

@nicbet
Created July 8, 2019 23:41
Show Gist options
  • Save nicbet/d6230db940f8589db3545f8a64323168 to your computer and use it in GitHub Desktop.
Save nicbet/d6230db940f8589db3545f8a64323168 to your computer and use it in GitHub Desktop.
Digital Ocean - Dynamic DNS (DDNS) Updater Script for Multiple Subdomains (e.g., Traefik)
#!/bin/bash
# Created by fibergames.net // Loranth Moroz // v.0.5
# Updated by yukicreative // Jay Vogt // v.0.6
# Updated by nicbet // Nicolas Bettenburg // v.0.7
# Required tools to run this script as is: curl (https://curl.haxx.se/) & jq (https://stedolan.github.io/jq/)
# This is to be used with crontab -> example entry to run it every 3hours:
# 0 */3 * * * sh /path/to/script/dnsupdater.sh
# Don't forget to make it executable: chmod +x /path/to/script/dnsupdater.sh
# Edit token, domain, subdomain to fit your needs
token=${DOTOKEN}
domain=${DOMAIN}
subdomains=${SUBDOMAINS}
# Normally an A Record. Change if needing to update a different record type
recordtype="A"
# Substitute ipinfo.io with your own ip-checker e.g. ipecho.net/plain
ip=$(curl --silent ipinfo.io/ip)
for subdomain in $subdomains; do
record_id=$(curl --silent --request GET --header "Content-Type: application/json" --header "Authorization: Bearer $token" "https://api.digitalocean.com/v2/domains/$domain/records" | jq ".[] | . [] | select(.name==\"${subdomain}\") | select(.type==\"${recordtype}\")" 2>/dev/null | grep "id" | sed --regexp-extended "s/[^0-9]//g");
curl --silent -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $token" -d '{"data":"'$ip'"}' "https://api.digitalocean.com/v2/domains/$domain/records/$record_id" > /dev/null;
echo -e "==DNS for $subdomain.$domain updated with IP: $ip==";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment