Open truefriend-cz opened 2 years ago
I can confirm that script doesn't work on RouterOS 7.5. All values looks like correct, PowerShell script from my Windows machine can change DNS record data using the same values (token, zone and record IDs etc), but Mikrotik with this script can't. There's no any error written to Mikrotik log, it looks like the script worked fine, but in fact there's no any changes in DNS records.
Hmm.. Im not too sure how to debug your issues but im running RouterOS 7.6 at home and other clients with no issues. Try manually creating a ddns.temp.txt file on your pc and just type in "1.1.1.1" and save it like that and upload it to your mikrotik. Run the script and check for changes.
There's no problem with temp file creation. I've just upgraded my RouterOS to the latest 7.6 version and it looks like the script is working now. I don't know what was a problem, but I didn't edit script. It simply works after Mikrotik was upgraded and rebooted. Thanks.
I got the same problem on ROS 7.7 version with level 6 license... all the log output is normal without any errors... but the DNS record on CF didn't get refreshed... can't tell what happened.
The main issue i encounter with the script is that it fails the very first update after you start it for the first time. Try to manually create the temp.ddns.txt file and put in 1.1.1.1 or something similar as long as its not your public IP. Remove the old one from Mikrotik and upload this new one. I will take my time finally to update this script to work better later today hopefully.
From my end, seems like it failed to send any API call to Cloudflare. And RouterOS doesn't have any response feedback makes things worse. The temp.ddns.file is generating correctly from my end at least. I will try your resolustion later. thanks for your quick response!
Hope fully later ill try to update the whole script so you dont even have to get the record ID anymore and some more validation hopefully. Check back again
I just tried to replace temp.ddns.txt but didn't get any difference. the log looks like quite normal however cloudflare doesn't know what happened outside...
Hmm current IP is blank also for you. What does your script config look like?
Make sure to hide your API keys and/or any personal identifiers
################# CloudFlare variables ################# :local CFDebug "true" :local CFcloud "false"
:global WANInterface "pppoe"
:local CFdomain "xxx"
:local CFtkn "xxx"
:local CFzoneid "xxx" :local CFid "xxx"
:local CFrecordType "" :set CFrecordType "A"
:local CFrecordTTL "" :set CFrecordTTL "120"
######################################################################### ######################## DO NOT EDIT BELOW ############################ #########################################################################
:log info "Updating $CFDomain ..."
################# Internal variables ################# :local previousIP "" :global WANip ""
################# Build CF API Url (v4) ################# :local CFurl "https://api.cloudflare.com/client/v4/zones/" :set CFurl ($CFurl . "$CFzoneid/dns_records/$CFid");
################# Get or set previous IP-variables ################# :if ($CFcloud = "true") do={ :set WANip [/ip cloud get public-address] };
:if ($CFcloud = "false") do={ :local currentIP [/ip address get [/ip address find interface=$WANInterface ] address]; :set WANip [:pick $currentIP 0 [:find $currentIP "/"]]; };
:if ([/file find name=ddns.tmp.txt] = "") do={
:log error "No previous ip address file found, createing..."
:set previousIP $WANip;
:execute script=":put $WANip" file="ddns.tmp";
:log info ("CF: Updating CF, setting $CFDomain = $WANip")
/tool fetch http-method=put mode=https output=none url="$CFurl" http-header-field="Authorization:Bearer $CFtkn,content-type:application/json" http-data="{\"type\":\"$CFrecordType\",\"name\":\"$CFdomain\",\"ttl\":$CFrecordTTL,\"content\":\"$WANip\"}"
:error message="No previous ip address file found."
} else={
:if ( [/file get [/file find name=ddns.tmp.txt] size] > 0 ) do={
:global content [/file get [/file find name="ddns.tmp.txt"] contents] ;
:global contentLen [ :len $content ] ;
:global lineEnd 0;
:global line "";
:global lastEnd 0;
:set lineEnd [:find $content "\n" $lastEnd ] ;
:set line [:pick $content $lastEnd $lineEnd] ;
:set lastEnd ( $lineEnd + 1 ) ;
:if ( [:pick $line 0 1] != "#" ) do={
:set previousIP [:pick $line 0 $lineEnd ];
:set previousIP [:pick $previousIP 0 [:find $previousIP "\r"]];
}
}
}
######## Write debug info to log ################# :if ($CFDebug = "true") do={ :log info ("CF: hostname = $CFdomain") :log info ("CF: previousIP = $previousIP") :log info ("CF: currentIP = $currentIP") :log info ("CF: WANip = $WANip") :log info ("CF: CFurl = $CFurl&content=$WANip") :log info ("CF: Command = \"/tool fetch http-method=put mode=https url=\"$CFurl\" http-header-field="Authorization:Bearer $CFtkn,content-type:application/json" output=none http-data=\"{\"type\":\"$CFrecordType\",\"name\":\"$CFdomain\",\"ttl\":$CFrecordTTL,\"content\":\"$WANip\"}\"") };
######## Compare and update CF if necessary ##### :if ($previousIP != $WANip) do={ :log info ("CF: Updating CF, setting $CFDomain = $WANip") /tool fetch http-method=put mode=https url="$CFurl" http-header-field="Authorization:Bearer $CFtkn,content-type:application/json" output=none http-data="{\"type\":\"$CFrecordType\",\"name\":\"$CFdomain\",\"ttl\":$CFrecordTTL,\"content\":\"$WANip\"}" /ip dns cache flush :if ( [/file get [/file find name=ddns.tmp.txt] size] > 0 ) do={ /file remove ddns.tmp.txt :execute script=":put $WANip" file="ddns.tmp" } } else={ :log info "CF: No Update Needed!" }
that's it. I thought current IP should be blank cause next line is WAN IP haha. And my IP showed in WAN IP correctly.
I also had several issues with this script, and also solved those:
[/ip address get [/ip address find interface=$WANInterface ] This line returned an array in my case. So I had to encapsulate this with [:pick ... 0]
currentIP is for me local, i.e. outside the if-clause its undefined (empty). I solved this by adding a new line just after previousIP to define currentIP: :local currentIP "" and i.s.o. local use set to set currentIP in the if-clause: :set currentIP [/ip ...
The instance variables are case sensitive, so I had to change: :log info "Updating $CFdomain ..." (Note CFdomain is written here with a lower case 'd')
I can create a PR, if this is still active.
Doesn't work for me. Please update to new version script.