Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions get-dns.sh
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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":".*?"'
19 changes: 19 additions & 0 deletions get-domain-ids.sh
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 3 additions & 1 deletion secrets.example
Original file line number Diff line number Diff line change
@@ -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)

73 changes: 61 additions & 12 deletions update-dns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand All @@ -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 \
Expand All @@ -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