From c579f800e80b6093a64e9fbfa55a3b811bdf9e1b Mon Sep 17 00:00:00 2001 From: thystra Date: Sat, 19 Jul 2025 13:19:09 -0400 Subject: [PATCH 1/4] Create get-domain-ids.sh Script to pull and extract domain IDs. Changed grep from -E to -P and now it works properly. --- get-domain-ids.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 get-domain-ids.sh diff --git a/get-domain-ids.sh b/get-domain-ids.sh new file mode 100644 index 0000000..e4b1665 --- /dev/null +++ b/get-domain-ids.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +source ./secrets + +read -p "Please enter the domain (e.g. example.com): " DOMAIN2 +read -p "Please enter the submain (e.g. host.example.com): " SUBDOMAIN + + +response=$(curl \ + --silent \ + -X GET \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $ACCESS_TOKEN" \ + "https://api.digitalocean.com/v2/domains/$DOMAIN2/records?name=$SUBDOMAIN") + +echo "$response" | grep -Po '"id":\d*|"type":"\w*"|"name":"\w*"|"data":".*?"' + +#Use this line to see the raw output if the above doesn't work on your system: +#curl -X GET -H "Authorization: Bearer $ACCESS_TOKEN " "https://api.digitalocean.com/v2/domains/argentwolf.org/records?name=$SUBDOMAIN" From 5554e630e98d4bf0c17cebce6383e2aef0b3fde7 Mon Sep 17 00:00:00 2001 From: thystra Date: Sat, 19 Jul 2025 13:20:46 -0400 Subject: [PATCH 2/4] Update update-dns.sh - Change to support both IPv4 and IPv6 - Change API check from account to Domain so it doesn't fail on a limited access API key --- update-dns.sh | 73 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/update-dns.sh b/update-dns.sh index 5a1210c..7296e99 100755 --- a/update-dns.sh +++ b/update-dns.sh @@ -6,14 +6,21 @@ source ./secrets -# Exit if the RECORD_IDS array has no elements -[ ${#RECORD_IDS[@]} -eq 0 ] && \ - echo 'RECORD_IDS are missing!' && \ +# Exit if the RECORD6_IDS array has no elements +[ ${#RECORD4_IDS[@]} -eq 0 ] && \ + echo 'RECORD4_IDS are missing!' && \ exit 1 +# Exit if the RECORD6_IDS array has no elements +[ ${#RECORD6_IDS[@]} -eq 0 ] && \ + echo 'RECORD6_IDS are missing!' && \ + exit 1 + +#https://api.digitalocean.com/v2/domains/ + # Function to check if the ACCESS_TOKEN is valid check_credentials() { - response=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://api.digitalocean.com/v2/account") + response=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://api.digitalocean.com/v2/domains") if [ "$response" != "200" ]; then echo "Invalid credentials. Please check your ACCESS_TOKEN." exit 1 @@ -24,10 +31,13 @@ check_credentials() { check_credentials -public_ip=$(curl -s http://checkip.amazonaws.com/) +public_ip4=$(curl -4 ifconfig.me) +public_ip6=$(curl ifconfig.me) + +echo "Processing IPv4 records..." -for ID in "${RECORD_IDS[@]}"; do - local_ip=$( +for ID4 in "${RECORD4_IDS[@]}"; do + local_ip4=$( curl \ --fail \ --silent \ @@ -39,13 +49,15 @@ for ID in "${RECORD_IDS[@]}"; do grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' ) + # if the IPs are the same just exit - if [ "$local_ip" == "$public_ip" ]; then - echo "IP has not changed for record ${ID}, skipping." + if [ "$local_ip4" == "$public_ip4" ]; then + echo "IP has not changed for record ${ID4}, skipping." continue fi - echo "Updating DNS record ${ID} with new IP address: ${public_ip}" + + echo "Updating DNS record ${ID4} with new IP address: ${public_ip4}" # --fail silently on server errors curl \ --fail \ @@ -54,7 +66,44 @@ for ID in "${RECORD_IDS[@]}"; do -X PUT \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ - -d "{\"data\": \"${public_ip}\"}" \ - "https://api.digitalocean.com/v2/domains/${DOMAIN}/records/${ID}" + -d "{\"data\": \"${public_ip4}\"}" \ + "https://api.digitalocean.com/v2/domains/${DOMAIN}/records/${ID4}" + + +done + +#ipv6 loop +echo "Processing IPv6 records..." + +for ID6 in "${RECORD6_IDS[@]}"; do + local_ip6=$( + curl \ + --fail \ + --silent \ + -X GET \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${ACCESS_TOKEN}" \ + "https://api.digitalocean.com/v2/domains/${DOMAIN}/records/${ID6}" | \ + grep -Eo '"data":".*?"' | \ + grep -Eo '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' + ) + + # if the IPs are the same just exit + if [ "$local_ip6" == "$public_ip6" ]; then + echo "IP has not changed for record ${ID6}, skipping." + continue + fi + + echo "Updating DNS record ${ID6} with new IP address: ${public_ip6}" + # --fail silently on server errors + curl \ + --fail \ + --silent \ + --output /dev/null \ + -X PUT \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${ACCESS_TOKEN}" \ + -d "{\"data\": \"${public_ip6}\"}" \ + "https://api.digitalocean.com/v2/domains/${DOMAIN}/records/${ID6}" done From 360a3b4df8caf960c2c11d71a2d61af4a8808b1d Mon Sep 17 00:00:00 2001 From: thystra Date: Sat, 19 Jul 2025 13:21:55 -0400 Subject: [PATCH 3/4] Update secrets.example Separate IPv4 and IPv6 IDs. --- secrets.example | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/secrets.example b/secrets.example index 369e817..450b2cb 100644 --- a/secrets.example +++ b/secrets.example @@ -1,4 +1,6 @@ ACCESS_TOKEN=your_token DOMAIN=example.com # The IDs of the record(s) you want to update, separated with spaces -RECORD_IDS=(12345 23456) +RECORD4_IDS=(0000000000 0000000000) +RECORD6_IDS=(0000000000 0000000000) + From 27dba05852e9fbc64e716997763b421b5342d2e4 Mon Sep 17 00:00:00 2001 From: thystra Date: Sat, 19 Jul 2025 13:24:47 -0400 Subject: [PATCH 4/4] Update get-dns.sh -change grep from -E to -P --- get-dns.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/get-dns.sh b/get-dns.sh index 2c27f18..45f0814 100755 --- a/get-dns.sh +++ b/get-dns.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash - +#This script will pull ALL domain records for the specified domain in the *secrets* file source ./secrets response=$(curl \ @@ -9,4 +9,4 @@ response=$(curl \ -H "Authorization: Bearer $ACCESS_TOKEN" \ "https://api.digitalocean.com/v2/domains/$DOMAIN/records") -echo "$response" | grep -Eo '"id":\d*|"type":"\w*"|"name":"\w*"|"data":".*?"' +echo "$response" | grep -Po '"id":\d*|"type":"\w*"|"name":"\w*"|"data":".*?"'