Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please update this script #4

Open
truefriend-cz opened this issue Feb 19, 2022 · 12 comments
Open

Please update this script #4

truefriend-cz opened this issue Feb 19, 2022 · 12 comments

Comments

@truefriend-cz
Copy link

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

@wein72
Copy link

wein72 commented Jan 3, 2023

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
Copy link
Owner

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
Copy link

wein72 commented Jan 3, 2023

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
Copy link

elfkall commented May 3, 2023

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
Copy link
Owner

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
Copy link

elfkall commented May 3, 2023

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
Copy link
Owner

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
Copy link

elfkall commented May 3, 2023

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
Copy link
Owner

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
Copy link

elfkall commented May 3, 2023

################# 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
Copy link

elfkall commented May 3, 2023

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
Copy link

erwin-kok commented Dec 27, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants