-
-
Save eaxsi/0f1a27aa83b73d4df7f6d70edae8d6ba to your computer and use it in GitHub Desktop.
Update a DNS record dynamically on DigitalOcean with a Shell Script
This file contains hidden or 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 | |
| ######################################################################### | |
| # # | |
| # DNS A & AAAA Record Updater # | |
| # # | |
| # Purpose: Update DNS A and AAAA Records on DigitalOcean via the API # | |
| # # | |
| # Description: Modified version of original update-do-dns.sh # | |
| # https://gist.github.com/V1ncNet/cc7615c6b6bde7248d6c5bc9e0335d6d # | |
| # This script retrieves the current public IPv4 and IPv6 # | |
| # addresses, stores them in .curip and .curip6 files. If # | |
| # the curled addresses don't match their former values, # | |
| # the script sends PUT requests to update both A and AAAA # | |
| # records. The new addresses are stored in the respective # | |
| # files afterward. # | |
| # # | |
| # Usage: Insert your API key, domain id and domain name, then run the # | |
| # script. Don't forget to run chmod +x update-do-dns.sh before. # | |
| # # | |
| # Usecase: Access local network from the internet via a domain name. # | |
| # Crontab executes this script every 15 minutes. # | |
| # # | |
| ######################################################################### | |
| DIGITALOCEAN_TOKEN="" # <- Insert your DO API key here | |
| DOMAIN_ID_A="" # <- Insert your domain A record id here | |
| DOMAIN_ID_AAAA="" # <- Insert your domain AAAA record id here | |
| DOMAIN="" # <- Insert your domain name here | |
| PWD="$(dirname $0)" | |
| FILE_IP="$PWD/.curip" | |
| FILE_IP6="$PWD/.curip6" | |
| # Check for existing files for IPv4 and IPv6 | |
| if [ -e "$FILE_IP" ]; then | |
| CURRENT_IP=$(awk '{print $1;}' $FILE_IP) | |
| else | |
| echo " *** $FILE_IP does not exist. Creating a new one" | |
| touch $FILE_IP | |
| fi | |
| if [ -e "$FILE_IP6" ]; then | |
| CURRENT_IP6=$(awk '{print $1;}' $FILE_IP6) | |
| else | |
| echo " *** $FILE_IP6 does not exist. Creating a new one" | |
| touch $FILE_IP6 | |
| fi | |
| # Function to retrieve IPv4 | |
| get_ipv4() { | |
| echo " *** Curl for current public IPv4 address from ipecho.net" | |
| IP=$(curl -4 -s -m 60 http://ipecho.net/plain) | |
| if [ $? -ne 0 ] || [ -z "$IP" ]; then | |
| echo " --- ipecho.net failed. Fallback to icanhazip.com" | |
| IP=$(curl -4 -s -m 60 http://ipv4.icanhazip.com) | |
| if [ $? -ne 0 ]; then | |
| echo " --- Timeout. The operation took too long" | |
| exit 1 | |
| fi | |
| fi | |
| } | |
| # Function to retrieve IPv6 | |
| get_ipv6() { | |
| echo " *** Curl for current public IPv6 address from ipecho.net" | |
| IP6=$(curl -6 -s -m 60 http://ipecho.net/plain) | |
| if [ $? -ne 0 ] || [ -z "$IP6" ]; then | |
| echo " --- ipecho.net failed. Fallback to icanhazip.com" | |
| IP6=$(curl -6 -s -m 60 https://ipv6.icanhazip.com) | |
| if [ $? -ne 0 ]; then | |
| echo " --- Timeout. The operation took too long" | |
| exit 1 | |
| fi | |
| fi | |
| } | |
| # Retrieve IPv4 and IPv6 addresses | |
| get_ipv4 | |
| get_ipv6 | |
| # Update A record if IPv4 has changed | |
| if [ "$IP" != "$CURRENT_IP" ]; then | |
| echo " *** Send PUT request to update A record on DigitalOcean API v2" | |
| curl -s -m 60 -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" -d '{"data":"'"$IP"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$DOMAIN_ID_A" | |
| if [ $? -ne 0 ]; then | |
| echo " --- Timeout. The operation took too long" | |
| exit 1 | |
| fi | |
| echo " *** Write new public IPv4 to $FILE_IP" | |
| echo $IP > $FILE_IP | |
| echo " *** Successfully updated A record" | |
| else | |
| echo " *** IPv4 address hasn't changed." | |
| fi | |
| # Update AAAA record if IPv6 has changed | |
| if [ "$IP6" != "$CURRENT_IP6" ]; then | |
| echo " *** Send PUT request to update AAAA record on DigitalOcean API v2" | |
| curl -s -m 60 -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" -d '{"data":"'"$IP6"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$DOMAIN_ID_AAAA" | |
| if [ $? -ne 0 ]; then | |
| echo " --- Timeout. The operation took too long" | |
| exit 1 | |
| fi | |
| echo " *** Write new public IPv6 to $FILE_IP6" | |
| echo $IP6 > $FILE_IP6 | |
| echo " *** Successfully updated AAAA record" | |
| else | |
| echo " *** IPv6 address hasn't changed." | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment