-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddns.sh
executable file
·63 lines (50 loc) · 2.11 KB
/
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/sh
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2024 Stephan Hegemann
set -e
# For debugging. Do not set this in production use, because this would print your api token to the journal / terminal
#set -x
# Config
domain_names='my-domain.com
subdomain1.my-domain.com
subdomain2.my-domain.com'
zone_id=''
api_token=''
ipv4=$(curl --no-progress-meter -4 https://cloudflare.com/cdn-cgi/trace | grep ip | cut -d = -f 2)
ipv6=$(curl --no-progress-meter -6 https://cloudflare.com/cdn-cgi/trace | grep ip | cut -d = -f 2)
records=$(curl --no-progress-meter --request GET --url https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records --header 'Content-Type: application/json' --header "Authorization: Bearer $api_token")
for domain in $domain_names
do
# A
record_id=$(echo $records | jq -r '.result[] | select(.name=="'"$domain"'" and .type=="A") | .id')
# If you don't explicitly set the proxied field, it would be set to "Not Proxied"
# So we set it explicitly on every update to what it should be
proxied=$(echo $records | jq -r '.result[] | select(.name=="'"$domain"'" and .type=="A") | .proxied')
curl --no-progress-meter \
--request PUT \
--url https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $api_token" \
--data "{
\"content\": \"$ipv4\",
\"name\": \"$domain\",
\"proxied\": $proxied,
\"type\": \"A\"
}"
# AAAA
record_id=$(echo $records | jq -r '.result[] | select(.name=="'"$domain"'" and .type=="AAAA") | .id')
# If you don't explicitly set the proxied field, it would be set to "Not Proxied"
# So we set it explicitly on every update to what it should be
proxied=$(echo $records | jq -r '.result[] | select(.name=="'"$domain"'" and .type=="AAAA") | .proxied')
curl --no-progress-meter \
--request PUT \
--url https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $api_token" \
--data "{
\"content\": \"$ipv6\",
\"name\": \"$domain\",
\"proxied\": $proxied,
\"type\": \"AAAA\"
}"
done