-
-
Save kiler129/3a436488ebc6bd79c233 to your computer and use it in GitHub Desktop.
| ######################################################################### | |
| # ================================================== # | |
| # $ 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 protected]' -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 protected]" | |
| :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!" | |
| } |
Anyone done it yet ? ;)
I managed to make it work and place it in Fork
unable to get CFid
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"
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:

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.
I have 2 ISPs for Failover. The gateways switching by the distance change. How to modify this script for 2 ISP/GW?
Anyone done it yet ? ;)