-
Notifications
You must be signed in to change notification settings - Fork 8
/
update-dns.sh
executable file
·60 lines (49 loc) · 1.55 KB
/
update-dns.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
[ ! -f ./secrets ] && \
echo 'secrets file is missing!' && \
exit 1
source ./secrets
# Exit if the RECORD_IDS array has no elements
[ ${#RECORD_IDS[@]} -eq 0 ] && \
echo 'RECORD_IDS are missing!' && \
exit 1
# 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")
if [ "$response" != "200" ]; then
echo "Invalid credentials. Please check your ACCESS_TOKEN."
exit 1
fi
}
# Check credentials before proceeding
check_credentials
public_ip=$(curl -s http://checkip.amazonaws.com/)
for ID in "${RECORD_IDS[@]}"; do
local_ip=$(
curl \
--fail \
--silent \
-X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
"https://api.digitalocean.com/v2/domains/${DOMAIN}/records/${ID}" | \
grep -Eo '"data":".*?"' | \
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."
continue
fi
echo "Updating DNS record ${ID} with new IP address: ${public_ip}"
# --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_ip}\"}" \
"https://api.digitalocean.com/v2/domains/${DOMAIN}/records/${ID}"
done