This is a script I use for dynamic DNS from RouterOS, using the Cloudflare DNS API. Inspired by bayukurnia.com.
- Get an API token from Cloudflare
- Get the identifier for your DNS zone, right sidebar
- Call the API to find out the record id
- Set up a DHCP client on your interface of choice
- Paste the script below
- Replace the Cloudflare variables with your values (see preparations)
- Enjoy dynamic DNS!
#--------------------------------------------
# MikroTik DDNS Script | Cloudflare API v4
# Inspired by https://bayukurnia.com/blog/mikrotik-ddns-cloudflare-api/
# See also https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
#--------------------------------------------
# Cloudflare variables, adjust with yours
:local cfToken "xxxxxx"
:local cfZoneId "zone_id"
:local cfDnsId "record_id"
:local cfDnsType "A"
:local cfDnsName "dyn.example.org"
:local cfDnsTTL "1"
:local cfDnsProxied "false"
:local cfApiUrl "https://api.cloudflare.com/client/v4/zones/$cfZoneId/dns_records/$cfDnsId"
:local cfHeaders "Authorization: Bearer $cfToken"
if ($bound=1) do={
:local newIp $"lease-address"
:log info "Updating dynamic dns: $dnsName is now $newIp"
# compose payload
:local cfPayload "{\"type\":\"$cfDnsType\",\"name\":\"$cfDnsName\",\"content\":\"$newIp\",\"ttl\":$cfDnsTTL,\"proxied\":$cfDnsProxied}"
:do {
:log info "Delaying to stabilize connection"
:delay 1500ms
:local response [/tool fetch http-method="put" url=$cfApiUrl http-header-field=$cfHeaders http-data=$cfPayload as-value output=user]
:if ($response->"status" = "finished") do={
:log info "DDNS: pointed $dnsName to $newIp"
:local message "Snickars has new ip: $newIp"
}
} on-error {
:log error "DDNS: failed to change $dnsName to $newIp"
}
}