DigitalOcean-DDNS-Bash

Visit site

Pure shell script to update IPv4 and IPv6 adresses records for domain hosted by DigitalOcean.

#!/bin/bash

#################### VARIABLES ####################
TOKEN="aaaaabbbbbccccc000001111122222"
DOMAIN="example.com"
RECORD_V4=("300000001" "300000002")
RECORD_V6=("300000003" "300000004")
###################################################

CURRENT_IPV4="$(curl -s v4.ident.me)"
CURRENT_IPV6="$(curl -s v6.ident.me)"

for RECORD_ID in ${RECORD_V4[*]}
do
	LAST_IPV4="$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"  | grep -Po '"data":(\d*?,|.*?[^\\]",)' | awk -F '"' '{print $4}')"
	HOST="$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"  | grep -Po '"name":(\d*?,|.*?[^\\]",)' | awk -F '"' '{print $4}')"
	if [ $CURRENT_IPV4 == $LAST_IPV4 ]; then
		echo "IPv4 has not changed for [$HOST] ($CURRENT_IPV4)"
	else
		echo "IPv4 for [$HOST] has changed to $CURRENT_IPV4 from $LAST_IPV4"
		curl -s -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"
		echo ""
	fi
done

for RECORD_ID in ${RECORD_V6[*]}
do
	LAST_IPV6="$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"  | grep -Po '"data":(\d*?,|.*?[^\\]",)' | awk -F '"' '{print $4}')"
	HOST="$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"  | grep -Po '"name":(\d*?,|.*?[^\\]",)' | awk -F '"' '{print $4}')"
	if [ $CURRENT_IPV6 == $LAST_IPV6 ]; then
		echo "IPv6 has not changed for [$HOST] ($CURRENT_IPV6)"
	else
		echo "IPv6 for [$HOST] has changed to $CURRENT_IPV6 from $LAST_IPV6"
		curl -s -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"data":"'"$CURRENT_IPV6"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"
		echo ""
	fi
done
Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Publish your Tool on Community

Have you created an Integration, API Wrapper, Service, or other Tool that helps developers build on DigitalOcean? Help users find it by listing it in Community Tools.

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel