Skip to content

Instantly share code, notes, and snippets.

@jayvogt
Forked from fibergames/dnsupdater.sh
Created August 29, 2018 16:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jayvogt/e32ffcd91e55ba1051c4f55b17a8a573 to your computer and use it in GitHub Desktop.
Save jayvogt/e32ffcd91e55ba1051c4f55b17a8a573 to your computer and use it in GitHub Desktop.
DigitalOcean dynamic DNS updater script for your subdomain
#!/bin/bash
# Created by fibergames.net // Loranth Moroz // v.0.5
# Updated by yukicreative // Jay Vogt // v.0.6
# Required tools to run this script as is: curl (https://curl.haxx.se/) & jq (https://stedolan.github.io/jq/)
# This only works for Digitalocean - 10$ credit referral link: https://m.do.co/c/fed75101475f
# Edit token, domain, subdomain to fit your needs
# Substitute ipinfo.io with your own ip-checker e.g. ipecho.net/plain
# 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
token="DO-Developer API_KEY"
domain="Your domain hosted at DO-nameservers"
subdomain="PREPARED subdomain via DO-Interface" #Use "@" for main record
recordtype="A" #Normally an A Record. Change if needing to update a different record type
ip=$(curl --silent ipinfo.io/ip)
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 "\n==DNS updated with IP: $ip=="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment