Community discussions

MikroTik App
 
mmendescortes
just joined
Topic Author
Posts: 1
Joined: Sat Mar 09, 2019 1:04 am

Cloudflare's DDNS

Sat Mar 09, 2019 1:16 am

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...

Code: Select all

: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.
 
mitry
just joined
Posts: 23
Joined: Tue Jun 26, 2012 10:06 pm

Re: Cloudflare's DDNS

Sun Jun 23, 2019 6:42 pm

Working like a charm, thanx a lot!
 
yujizmq
just joined
Posts: 2
Joined: Fri Feb 15, 2019 11:39 am

Re: Cloudflare's DDNS

Tue Jun 25, 2019 7:53 am

Thx for your script~
btw share some other alternatives i collected:
https://gist.github.com/asuna/4acb9521e ... d83b8b65df # compare previous interface ip address
https://gist.github.com/viritt/605173d0 ... 539db9923f # compare previous domain A record ip address
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Cloudflare's DDNS

Tue Jun 25, 2019 1:06 pm

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
 
DerAlSem
just joined
Posts: 7
Joined: Sun Jul 23, 2017 1:25 pm

Re: Cloudflare's DDNS

Sat Jun 29, 2019 3:05 pm

/ip cloud doesn't support ipv6, does it?
 
katsuo
just joined
Posts: 1
Joined: Sun Nov 03, 2019 5:37 am

Re: Cloudflare's DDNS

Sun Nov 03, 2019 5:42 am

thanks for the script but i cant get it working on my mikrotik,routeros is up to date.
any clue??
 
nsitadmin
just joined
Posts: 6
Joined: Mon Jan 15, 2018 8:46 am

Re: Cloudflare's DDNS

Fri Dec 03, 2021 8:29 am

Good day

I see the link provided for the tutorial does not work can you guide me through it
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Cloudflare's DDNS

Fri Dec 03, 2021 9:09 am

/ip cloud doesn't support ipv6, does 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.
 
gfhahsvhj
just joined
Posts: 9
Joined: Fri Jan 24, 2020 3:30 pm

Re: Cloudflare's DDNS

Wed Mar 09, 2022 3:51 pm

How to update multiple subdomains?

EDIT: nvm, i use cname entries
 
ccl13
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Sun Dec 24, 2017 8:20 pm

Re: Cloudflare's DDNS

Thu Jan 18, 2024 8:46 pm

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/funda ... ate-token/) which is safer and more flexible. Plus some minor quality of life changes.
 
audelair
just joined
Posts: 5
Joined: Sat Jan 20, 2024 9:46 am

Re: Cloudflare's DDNS

Mon Jan 22, 2024 6:50 pm

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/funda ... ate-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!

Who is online

Users browsing this forum: No registered users and 20 guests