mike6715b / Mikrotik_CF_DDNS

Script for automatic DNS updates on Cloudflare
64 stars 25 forks source link

Please update this script #4

Open truefriend-cz opened 2 years ago

truefriend-cz commented 2 years ago

Doesn't work for me. Please update to new version script.

wein72 commented 1 year 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.

mike6715b commented 1 year ago

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.

wein72 commented 1 year ago

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.

elfkall commented 1 year ago

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.

mike6715b commented 1 year ago

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.

elfkall commented 1 year ago

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!

mike6715b commented 1 year ago

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

elfkall commented 1 year ago

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

mike6715b commented 1 year ago

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

elfkall commented 1 year ago

################# 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={

:local previousIP [:pick $line 0 $lineEnd ]

            :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!" }

elfkall commented 1 year ago

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.

erwin-kok commented 10 months ago

I also had several issues with this script, and also solved those:

I can create a PR, if this is still active.