Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DigitalOcean dynamic DNS updater script for your subdomain
#!/bin/bash
# Created by fibergames.net // Loranth Moroz // v.0.5
# 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"
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}\")" 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=="
@jcanfield

This comment has been minimized.

Copy link

jcanfield commented Jul 15, 2018

Thank you. That worked perfectly. I'm running a Dell PowerEdge server at home and setup Nextcloud. My domain is being managed via DO, so this was exactly what I was looking for, a quick and clean Bash script. 👍

@yukicreative

This comment has been minimized.

Copy link

yukicreative commented Aug 22, 2018

That was crazy easy. Thanks for the help!

@yukicreative

This comment has been minimized.

Copy link

yukicreative commented Aug 29, 2018

I ran into issues with this when trying to use the @ record, as it would pull not only the A record, but the NS records as well. I've created an updated gist that allows you to select the record type here: https://gist.github.com/yukicreative/e32ffcd91e55ba1051c4f55b17a8a573

@fibergames

This comment has been minimized.

Copy link
Owner Author

fibergames commented Nov 11, 2018

I ran into issues with this when trying to use the @ record, as it would pull not only the A record, but the NS records as well. I've created an updated gist that allows you to select the record type here: https://gist.github.com/yukicreative/e32ffcd91e55ba1051c4f55b17a8a573

nice! great w0rk!

@gtsili

This comment has been minimized.

Copy link

gtsili commented Jan 7, 2020

Thank you for sharing,
I wanted something that can execute more frequently therefore, I have made some minor changes so that the script will check if the DNS record needs updating or not: https://gist.github.com/gtsili/9248e7934809ecc940e85bb3ae1d985b

Have a nice day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.