-
Notifications
You must be signed in to change notification settings - Fork 2
/
cloudflare-ddns.sh
63 lines (50 loc) · 2.1 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
59
60
61
62
63
#!/bin/bash
# Forked from lifehome/systemd-cfddns/src/cfupdater-v4
# CHANGE THESE
auth_key="xxxxx" # Top right corner, go to "My profile" > "API token" and generate new token allowing access to read and edit DNS.
zone_identifier="xxxxx" # Can be found in the "Overview" tab of your domain
record_name="foo.example.com" # Which record you want to be synced
keep_log=true
# Fetch Global IP address
ip=$(curl -4s ifconfig.co)
# Or you can use IP address from a specific interface
# ip=$(ip address show pppoe0 | grep -Po 'inet \K[\d.]+')
# 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 "Authorization: Bearer $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 "[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 address 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 "Authorization: Bearer $auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1}")
# Log IP address if it has changed
if "${keep_log}"; then
currenttime=$(date '+%Y-%m-%dT%H:%M%z')
echo "$currenttime, $ip" >> ip-address.txt
fi
# Result
success=$(echo $update | jq -r '.success')
case "$success" in
"true")
echo "[Cloudflare DDNS] IPv4 context '$ip' has been synced to Cloudflare.";;
*)
>&2 echo "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update"
exit 1;;
esac