forked from mu373/cloudflare-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare-ddns.sh
58 lines (45 loc) · 2.01 KB
/
cloudflare-ddns.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
#!/bin/bash
# Forked from lifehome/systemd-cfddns/src/cfupdater-v4
# CHANGE THESE
auth_email="[email protected]" # The email used to login 'https://dash.cloudflare.com'
auth_key="xxxxx" # Top right corner, "My profile" > "Global API Key"
zone_identifier="xxxxx" # Can be found in the "Overview" tab of your domain
record_name="home.examlpe.com" # Which record you want to be synced
# Fetch external IP address
ip=$(curl -s inet-ip.info)
# Seek for the current record on Cloudflare
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json")
# Can't do anything without the record
record_count=$(echo $record | jq -r '.result_info.count')
if [ $record_count == 0 ]; then
>&2 echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?"
exit 1
fi
# Extract existing IP address from the fetched record
old_ip=$(echo $record | jq -r '.result[0].content')
echo "[Cloudflare DDNS] Currently set to $old_ip"
# Compare if they're the same
if [ "$ip" == "$old_ip" ]; then
echo "[Cloudflare DDNS] IP has not changed."
exit 0
fi
# Extract record identifier from result
record_identifier=$(echo $record | jq -r '.result[0].id')
# Update IP address through Cloudflare API
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1}")
# Result
success=$(echo $update | jq -r '.success')
case "$success" in
"true")
echo "[Cloudflare DDNS] IPv4 context '$ip' has been synced to Cloudflare.";;
*)
>&2 echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update"
exit 1;;
esac