Skip to content

Instantly share code, notes, and snippets.

@kiler129
Last active September 24, 2025 04:46
Show Gist options
  • Select an option

  • Save kiler129/3a436488ebc6bd79c233 to your computer and use it in GitHub Desktop.

Select an option

Save kiler129/3a436488ebc6bd79c233 to your computer and use it in GitHub Desktop.
Automatic script for Mikrotik RouterOS updating record on CloudFlare.
#########################################################################
# ================================================== #
# $ Mikrotik RouterOS update script for CloudFlare $ #
# ================================================== #
# #
# - You need a CloudFlare account & api key (look under settings), #
# a zone and A record in it #
# - All variables in first section are obvious, except CFid, #
# To obtain CFid use following command in any unix shell: #
# curl https://www.cloudflare.com/api_json.html -d 'a=rec_load_all' -d 'tkn=YOUR_API_KEY' -d 'email=email@example.com' -d 'z=domain.com'|python -mjson.tool
# - Enable CFDebug if needed - it'll print some info to logs #
# - Put script under /system scripts giving "read" policy access. #
# For 6.29 and older "test" policy is also needed. #
# - Add script to /system scheduler using it's name in "on-event" #
# #
# Credits for Samuel Tegenfeldt, CC BY-SA 3.0 #
# Modified by kiler129 #
#########################################################################
################# CloudFlare variables #################
:local CFDebug "true"
:global WANInterface "ether1-gateway"
:local CFdomain "sub.domain.com"
:local CFzone "domain.com"
:local CFemail "email@example.com"
:local CFtkn "YOUR_API_KEY"
:local CFid "353275870"
:local CFrecordType "A"
:local CFserviceMode "0"
:local CFttl "120"
#########################################################################
######################## DO NOT EDIT BELOW ############################
#########################################################################
################# Internal variables #################
:local resolvedIP ""
:global WANip ""
################# Resolve and set IP-variables #################
:local currentIP [/ip address get [/ip address find interface=$WANInterface ] address];
:set WANip [:pick $currentIP 0 [:find $currentIP "/"]];
:set resolvedIP [:resolve $CFdomain];
################# Build CF API Url #################
:local CFurl "https://www.cloudflare.com/api_json.html\3F"
:set CFurl ($CFurl . "email=$CFemail&tkn=$CFtkn&a=rec_edit");
:set CFurl ($CFurl . "&id=$CFid&z=$CFzone&name=$CFdomain");
:set CFurl ($CFurl . "&type=$CFrecordType&service_mode=$CFserviceMode&ttl=$CFttl");
######## Write debug info to log #################
:if ($CFDebug = "true") do={
:log info ("CF: hostname = $CFdomain")
:log info ("CF: resolvedIP = $resolvedIP")
:log info ("CF: currentIP = $currentIP")
:log info ("CF: WANip = $WANip")
:log info ("CF: CFurl = $CFurl&content=$WANip")
};
######## Compare and update CF if necessary #####
:if ($resolvedIP != $WANip) do={
:log info ("CF: Updating CF, setting $CFDomain = $WANip")
/tool fetch mode=https url="$CFurl&content=$WANip" keep-result=no
/ip dns cache flush
} else={
:log info "CF: No Update Needed!"
}
@Arcanum417
Copy link
Copy Markdown

Anyone done it yet ? ;)

@viritt
Copy link
Copy Markdown

viritt commented Feb 26, 2019

Anyone done it yet ? ;)

I managed to make it work and place it in Fork

Copy link
Copy Markdown

ghost commented Feb 15, 2020

unable to get CFid

Copy link
Copy Markdown

ghost commented Feb 15, 2020

curl -X GET "https://api.cloudflare.com/client/v4/zones/[Zone ID]/dns_records"
-H "X-Auth-Email: [Email]"
-H "X-Auth-Key: [API Key]"
-H "Content-Type: application/json"

@kiler129
Copy link
Copy Markdown
Author

To be fair I never updated that script (and @viritt's fork mentioned above should work) because since introduction of MikroTik IP Cloud there's no point in doing so.

You can just set-up a CNAME on CF like so:
image

If it bothers you that there's an extra DNS query (I cannot think about any realistic scenario here) you can even enable CNAME Flattening and it will look like a standard A record.

@anc-tvr
Copy link
Copy Markdown

anc-tvr commented Nov 28, 2024

I have 2 ISPs for Failover. The gateways switching by the distance change. How to modify this script for 2 ISP/GW?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment