Cloudflare's DDNS

Script to update current existing cloudflare record with current public address, meant to be used as an cloud alternative for cloudflare’s ddns.

I’m posting it here because when I needed everything I found on google was links to another post that didn’t even have anything useful at all…

Tested and working with the RouterOS v6.44 on a hAP Lite. (tested on this post’s date!)

If you need to get your zone ID you can just curl a zone list and manually get the ID from the returned json as I did!

Not going into the merit of puting it as an script option, if you need help doing this just contact me and I will help, but it’s very simple to do and I don’t think anyone will really need a hand…
:global cfu do={
:local cfi “THE_ZONE_ID”;
:local cfr “THE_RECORD_ID”;
:local cfe “ACCOUNT_EMAIL_ADDRESS”;
:local cfk “API_GLOBAL_KEY”;
:local cfd “THE_RECORD_DOMAIN”;
:local cfa [/ip cloud get public-address];
:local cfp true;
/tool fetch mode=https
http-method=put
url=“https://api.cloudflare.com/client/v4/zones/$cfi/dns_records/$cfr
http-header-field=“content-type:application/json,X-Auth-Email:$cfe,X-Auth-Key:$cfk”
http-data=“{"type":"A","name":"$cfd","content":"$cfa","proxied":$cfp}”
output=none
}
=== UPDATE (03/04/2019) ===

If you need help setting it up, just follow this tutorial.

Working like a charm, thanx a lot!

Thx for your script~
btw share some other alternatives i collected:
https://gist.github.com/asuna/4acb9521e5e18dfc736b6dd83b8b65df # compare previous interface ip address
https://gist.github.com/viritt/605173d0b9d8cce433be34539db9923f # compare previous domain A record ip address

Or you can use MT own 100% free DNS
/ip cloud

Simple setup and works all the time.

You can then point your DNS to cloud DNS

www.mysite.com → “serial_RB”.sn.mynetname.net

/ip cloud doesn’t support ipv6, does it?

thanks for the script but i cant get it working on my mikrotik,routeros is up to date.
any clue??

Good day

I see the link provided for the tutorial does not work can you guide me through it

From the manual https://wiki.mikrotik.com/wiki/Manual:IP/Cloud
Since RouterOS v6.43 if your device is able to reach cloud2.mikrotik.com using IPv6, then a DNS AAAA record is going to be created for your public IPv6 address.

How to update multiple subdomains?

EDIT: nvm, i use cname entries

This is the first post on Google when searching for Mikrotik’s Cloudflare DDNS solutions, and I got my script’s original version here from OP. So adding my version here:

# Script name (must be same as you called the script)
:local scriptname "<YOUR_SCRIPT_NAME>"

:if ([:len [/system script job find script=$scriptname]]>1) do={
    log info "Another copy of $scriptname is running.";
    :error "Another copy of $scriptname is running.";
}
log info "Started $scriptname";

:local cfi "<YOUR_ZONE_ID>";
:local cfr "<YOUR_DNS_ENTRY_ID>";
:local ctk "<YOUR_API_TOKEN>";
:local cfd "<YOUR_TARGET_DOMAIN>";
:local cfa [/ip cloud get public-address];
:local cfp false;
/tool fetch mode=https\
http-method=put\
url="https://api.cloudflare.com/client/v4/zones/$cfi/dns_records/$cfr"\
http-header-field="content-type:application/json,Authorization:Bearer $ctk"\
http-data="{\"type\":\"A\",\"name\":\"$cfd\",\"content\":\"$cfa\",\"proxied\":$cfp}"\
output=file\
dst-path="$scriptname.txt"

The main change here is that I am using Cloudflare’s API token (https://developers.cloudflare.com/fundamentals/api/get-started/create-token/) which is safer and more flexible. Plus some minor quality of life changes.

Thanks for this… I was able to confirm this work’s great! I just added a quick check to see if the cfa variable matches the current public DNS setting, using this:

:resolve <domain name> server=1.1.1.1

Also added additional logging. And then wrapped it in an if/else statement. Scheduled it for every 30 minutes. Working really well!

This is harder than it should be!

I got this working on Router OS 7 to update record at Cloudflare:

https://gist.github.com/warwickchapman/6860c2387fa71c2391e51cff943eb351