Update A Cloudflare Zone Record Via Mikrotik Scripting

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Update a Cloudflare zone record via Mikrotik

scripting
Simone Ruffilli · Follow
1 min read · Jul 14, 2023

Listen Share

Scenario:

You have a dynamic, public WAN IP address, and a Mikrotik router somewhere in
your network.

You have a domain whose DNS is managed by Cloudflare, and you want to update a
specific record when your WAN IP changes.

You don’t want to manage a container/startup script to achieve your goal.

Solution:

Customize the local values in the script below, and use it as a scheduled script, running
every X minutes (X==5 for me).

:local apiKey "apikeyapikeyapikeyapikeyapikeyapikeyapikeyapikey"


:local cfUser "youremail@address"
:local zoneID "zoneIdzoneIdzoneIdzoneIdzoneIdzoneId"
:local recordID "recordIDrecordIDrecordIDrecordIDrecordID"
:local domainName "example.com"

# Do not edit below

:local currentIP ([/tool fetch url="https://ifconfig.me/ip" mode=https output=user


:local cloudflareDNSIP [:resolve $domainName server=1.1.1.1];

:if ($currentIP != $cloudflareDNSIP) do={


# If the IP has changed, update the Cloudflare record
:log info ("Updating Cloudflare record. Old IP: " . $cloudflareDNSIP . " New IP:

:local httpHeaders ("X-Auth-Email: " . $cfUser . "\r\nX-Auth-Key: " . $apiKey . "


:local payload ("{\"type\":\"A\",\"name\":\"" . $domainName . "\",\"content\":\""
/tool fetch mode=https url="https://api.cloudflare.com/client/v4/zones/$zoneID/dn
}

Future me looking for this solution: you’re welcome ;).

You might also like