forked from jgillman/digital-ocean-dynamic-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-dns.sh
executable file
·43 lines (35 loc) · 1.02 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
#!/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
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 '\d+\.\d+\.\d+\.\d+'
)
# if the IPs are the same just exit
[ "$local_ip" == "$public_ip" ] && exit 0
# --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