Skip to content

Instantly share code, notes, and snippets.

@calevans
Created January 28, 2022 21:47
Show Gist options
  • Save calevans/38d21031ad8af638eec698f97f6ba16d to your computer and use it in GitHub Desktop.
Save calevans/38d21031ad8af638eec698f97f6ba16d to your computer and use it in GitHub Desktop.
Update a record in a DNS Zone hosted at Digital Ocean
#!/bin/bash
#################### CHANGE THE FOLLOWING VARIABLES ####################
TOKEN="" // Digital Ocean API Token
DOMAIN="" // Domain Name
RECORD_ID="" // DO Assigned each record in a Zone file an ID. To update a record you need that ID.
LOG_FILE="/var/log/ips.txt"
########################################################################
CURRENT_IPV4="$(dig +short myip.opendns.com @resolver1.opendns.com)"
LAST_IPV4="$(tail -1 $LOG_FILE | awk -F, '{print $2}')"
if [ "$CURRENT_IPV4" = "$LAST_IPV4" ]; then
logger "$0 : IP has not changed ($CURRENT_IPV4)"
else
logger "$0 : IP has changed: $CURRENT_IPV4"
echo "$(date),$CURRENT_IPV4" >> "$LOG_FILE"
curl -o /dev/null -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"data":"'"$CURRENT_IPV4"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment