-
Notifications
You must be signed in to change notification settings - Fork 25
/
cloudflare_dynamic_ip_update.bash
executable file
·89 lines (78 loc) · 3.33 KB
/
cloudflare_dynamic_ip_update.bash
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
## Author: Hyecheol (Jerry) Jang
## Shell Script that check current public (dynamic) ip address of server,
## and update it to the Cloudflare DNS record after comparing ip address registered to Cloudflare
## basic shell scripting guide https://blog.gaerae.com/2015/01/bash-hello-world.html
## get current public IP address
currentIP=$(curl -s checkip.amazonaws.com)
if [[ $? == 0 ]] && [[ ${currentIP} ]]; then ## when dig command run without error,
## Making substring, only retrieving ip address of this server
## https://stackabuse.com/substrings-in-bash/
currentIP=$(echo $currentIP | cut -d'"' -f 2)
echo "current public IP address is "$currentIP
else ## error happens,
echo "Check your internet connection"
exit
fi
## Read configuration from separated config.json (created by configure.bash)
CONFIG_PATH=$(cd $(dirname $0) && pwd)"/config.json"
apiKey=$(jq -r '.api' $CONFIG_PATH)
name=($(jq -r '."update-target"[].name' $CONFIG_PATH))
id=($(jq -r '."update-target"[].id' $CONFIG_PATH))
zoneid=($(jq -r '."update-target"[].zone_id' $CONFIG_PATH))
unset CONFIG_PATH
# Error Checks
if [[ ${#name[@]} != ${#id[@]} ]]; then
echo "Config file Disrupted!!"
echo "Please re-generate config.json file (run configure.bash)"
exit
fi
if [[ ${#name[@]} != ${#id[@]} ]]; then
echo "Config file Disrupted!!"
echo "Please re-generate config.json file (run configure.bash)"
exit
fi
index=0
while [[ $index -lt ${#id[@]} ]]; do # For all update targets in config file
# Retrieve current DNS status
dnsStatusAPICall=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zoneid[index]}/dns_records/${id[index]}" \
-H "Authorization: Bearer $apiKey" \
-H "Content-Type:application/json" | jq .)
# Check for status
if [[ $(echo $dnsStatusAPICall | jq .success) != true ]] || [[ $(echo $dnsStatusAPICall | jq -r .result.name) != ${name[index]} ]]; then
echo "Error Occurred While Accessing Current DNS Status"
echo "May Caused by outdated config file. Please re-generate config.json file (run configure.bash)"
exit
fi
# compare recordIP with currentIP
if [[ $(echo $dnsStatusAPICall | jq -r .result.content) == $currentIP ]]; then
echo "${name[index]}: no needs to update"
else # Need to update
proxied=$(echo $dnsStatusAPICall | jq -r .result.proxied)
ttl=$(echo $dnsStatusAPICall | jq -r .result.ttl)
# JSON requestBody
data="{\"type\":\"A\",\"name\":\"${name[index]}\",\"content\":\"$currentIP\",\"ttl\":$ttl,\"proxied\":$proxied}"
unset proxied
unset ttl
# Update the entry
updateResult=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${zoneid[index]}/dns_records/${id[index]}" \
-H "Authorization: Bearer $apiKey" \
-H "Content-Type: application/json" \
--data $data | jq .)
unset data
# Check for result
if [[ $(echo $updateResult | jq -r .success) != true ]] || [[ $(echo $updateResult | jq -r .result.content) != $currentIP ]]; then
echo "Error While updating ${name[index]}"
else
echo "${name[index]}: successfully updated to $currentIP"
fi
unset updateResult
fi
index=$[$index+1]
unset dnsStatusAPICall
done
unset currentIP
unset apiKey
unset name
unset id
unset zoneid