mirror of
https://github.com/docker-home-server/mikrotik-cloudflare-dns.git
synced 2024-11-21 18:06:33 +02:00
49 lines
1.0 KiB
Bash
Executable File
49 lines
1.0 KiB
Bash
Executable File
#!/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
|