-
-
Save gtsili/9248e7934809ecc940e85bb3ae1d985b to your computer and use it in GitHub Desktop.
DigitalOcean dynamic DNS updater script for your subdomain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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=$(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) | |
record_data=$(echo "$record" | grep "data" | sed --regexp-extended "s/[^0-9\.]//g") | |
if [ "$ip" = "$record_data" ]; then | |
echo -e "\n==No DNS update is necessary==" | |
else | |
record_id=$(echo "$record" | 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==" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great. But would it be better to check if the IP address changed, by saving it as Previous, and if ip != Previous then use the api? This way you could check as often as you want, but only hit the api when it needs to update.