-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgen-script
executable file
·48 lines (38 loc) · 1.03 KB
/
gen-script
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
#!/bin/bash
HOST=api.cloudflare.com
if [ -z "$TOKEN" ]
then
echo TOKEN is not set. Cannot continue.
exit 2
fi
function dns_ids() {
curl -s "https://$HOST/client/v4/zones/$ZONE_ID/dns_records" \
-H "Accept: application/json" \
-H "Authorization: Bearer $TOKEN" \
| jq -r ".result[] | select(.name == (\"*.$DOMAIN\", \"$DOMAIN\")) | .id + \":\" + .name"
}
DNS_IDS=`dns_ids`
cat <<'EOT'
:global mcdPreviousIP;
:local mcdCurrentIP
:set mcdCurrentIP [/ip cloud get public-address]
:if ($mcdCurrentIP != $mcdPreviousIP) do={
:log info "mcd: new IP $mcdCurrentIP (was $mcdPreviousIP)"
:set mcdPreviousIP $mcdCurrentIP
EOT
for line in $DNS_IDS
do
id=${line%:*}
name=${line#*:}
cat <<EOT
/tool fetch mode=https \\
http-method=put \\
url="https://$HOST/client/v4/zones/$ZONE_ID/dns_records/$id" \\
http-header-field="content-type: application/json,Authorization: Bearer $TOKEN" \\
http-data="{\"type\":\"A\",\"name\":\"$name\",\"content\":\"\$mcdCurrentIP\"}" \\
output=none
EOT
done
cat <<EOT
}
EOT