0% found this document useful (0 votes)
68 views

Update A Cloudflare Zone Record Via Mikrotik Scripting

The document describes a solution to automatically update a DNS record on Cloudflare when a Mikrotik router's public IP address changes. It provides a script that can be run periodically to check the current public IP against what is configured in Cloudflare and update it if different.

Uploaded by

iwanbudakjambi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Update A Cloudflare Zone Record Via Mikrotik Scripting

The document describes a solution to automatically update a DNS record on Cloudflare when a Mikrotik router's public IP address changes. It provides a script that can be run periodically to check the current public IP against what is configured in Cloudflare and update it if different.

Uploaded by

iwanbudakjambi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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