I thought the following code will help those who have trouble setting up the DynDNS service with their Mikrotik router.
Although there are some scripts on the Mikrotik forum, there doesn't seem to be any official support and the Dyn instructions are very "poor".
So, I did some research, I collected the various scripts I found and finally I wrote my own script, that also has the ability to notify me whenever my public IP changes.
Of course you have to set up the email service in Mikrotik so that it can send you the notification.
The following script has been tested on ROS versions 6.49.15 and 7.14.3 on different devices and runs smoothly.
#---------- SCRIPT INFORMATION ------------------
#
# Script : DynDNS Update Script
# Version: 1.0.0
# Created: 21/05/2024
# Updated: 21/05/2024
# Author : SV1BGM
# Website: https://sv1bgm.blogspot.com
#
#--------- MODIFY AS NEEDED ---------------------
#
# DynDNS parameters (enter yours)
:local hostname "test.merseine.nu"
:local username "test"
:local password "test"
#
# DynDNS apiKey (in future version)
#:local key "apikey"
#
# Set true if you want to use IPv6
:local ipv6mode false;
#
#------------------------------------------------
#
# Free Online services IPv4
#
:local Service1 "https://api.ipify.org/"
:local Service2 "https://ipv4.icanhazip.com/"
#
# Free Online services IPv6
#
:local Service61 "https://api64.ipify.org/"
:local Service62 "https://ipv6.icanhazip.com/"
#
#------------------------------------------------
#
:local previousIP; :local currentIP; :local result
#
:log warning message="START: DynDNS Update"
#
# Check if ipv6 is enabled
#
if ($ipv6mode = true) do={
:set Service1 $Service61;
:set Service2 $Service62;
:log info "DynDNS: IPv6 mode enabled"
}
#
#------------------------------------------------
#
# Resolve stored DynDNS IP address
#
:do {:set previousIP [:resolve $hostname]} on-error={ :log error "DynDNS: Could NOT resolve hostname $hostname" };
#
#------------------------------------------------
#
# Detect current public IP address
#
:do {:set currentIP ([/tool fetch url=$Service1 output=user as-value]->"data")} on-error={
:log error "DynDNS: Service down-> $Service1"
# Second try in case the first one is failed
:do {:set currentIP ([/tool fetch url=$Service2 output=user as-value]->"data")} on-error={
:log error "DynDNS: Service down-> $Service2"
};
};
#
#-------------------------------------------------
#
# Here is the main script to update IP for the DynDNS host
#
:log info "DynDNS: Resolved IP : $previousIP, Current IP : $currentIP"
#
:if ($currentIP != $previousIP) do={
:log info "DynDNS: Current IP ($currentIP) is not equal to Resolved IP ($previousIP), update needed!";
:log warning message="DynDNS: Sending update for $hostname";
#
:local str "https://$username:$password@members.dyndns.org/nic/update?hostname=$hostname&myip=$currentIP";
:set result ([/tool fetch mode=https url=$str output=user as-value]->"data");
:log info "--- REPLY from server : $result";
/tool e-mail send to=test@test.com subject="Your IP Changed" body="New IP : $currentIP \r Old IP : $previousIP \r Response : $result"
#
} else={
:log warning message="DynDNS: No IP Change, No update needed";
}
:log warning message="DynDNS: END of process";
#
# End of script. Easy!
# ---------------------------------------------------
Several lines of the above code are just for you to see in the logs what is going on. You can safely delete them, but I like them there.
Obviously you should use the scheduler and set the script to run every 15 minutes or more, if your IP doesn't change frequently.
In no way do I claim to be an expert and I have no responsibility if this script does not "run" correctly and does not refresh your IP in the future. After all, Mikrotik has built-in capability and provides you with a free hostname; it's just not as visually nice, it's big and probably impossible to remember!
Feel free to send me your comments and suggestions to make this script better and with more features.
73, Fanis
SV1BGM