From 3004d35320b7a89d2fcaba2f19ab7cc52d707fe4 Mon Sep 17 00:00:00 2001 From: Miguel Scapolla Date: Thu, 29 Mar 2018 15:29:13 -0300 Subject: [PATCH] Update dyndns with Mikrotik. --- mikrotik/mikrotik-update-ipv4-dyndns.txt | 146 +++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 mikrotik/mikrotik-update-ipv4-dyndns.txt diff --git a/mikrotik/mikrotik-update-ipv4-dyndns.txt b/mikrotik/mikrotik-update-ipv4-dyndns.txt new file mode 100644 index 0000000..f08f3b9 --- /dev/null +++ b/mikrotik/mikrotik-update-ipv4-dyndns.txt @@ -0,0 +1,146 @@ +### Update IPv4 DynDns with a script in Mikrotik RouterOs +---------------------------------------------------------- + +This text explain how to update the dyndns information when you have a dynamic +IPv4 address in the WAN interface. + +The script uses this dyndns providers: + https://www.noip.com + https://www.duckdns.org + https://www.changeip.com + +Tested in RouterOS 6.41.3. + + +### Setup +---------- + +# Create the script with read and test permissions: + + /system script add name=DYNDNS policy=read,test + +# Paste the code: + + /system script edit DYNDNS source + +# Put the script in a scheduled job: + + /system scheduler add interval=5m name=DYNDNS policy=read,test + + +### Commented script +--------------------- + +#-- Name of the WAN interface with the dynamip IP address. +:local WANNAME "WAN"; + +#-- Fake IP address. When you have an ISP wich assign the same IP address for +#-- several days or weeks in every DHCP refresh, as there is no refreshment in +#-- the dyndns providers, the account is blocked. With this fake IP address, +#-- the script every day forces the change in dyndns for a little period of time +#-- so that the account does not get blocked. +:local DNSFAK "127.0.0.1"; + +#-- List of subdomains and API URL to refresh the IP address. +#-- Replace the uppercase fields for the correct values. +:local DNSALL { + ".duckdns.org"="https://www.duckdns.org/update?domains=&token=&ip="; + ".hopto.org"="https://:@dynupdate.no-ip.com/nic/update?hostname=.hopto.org&myip="; + ".changeip.org"="https://:@nic.changeip.com/nic/update?hostname=.changeip.org&myip="; + } + +#-- Actual hour and minute. +:local HOUR [:tonum [:pick [/system clock get time] 0 2]]; +:local MINU [:tonum [:pick [/system clock get time] 3 5]]; + +#-- Get the WAN IP address. +:local WANIP [/ip address get [/ip address find interface=$WANNAME disabled=no dynamic=yes] address]; +:set WANIP [:pick $WANIP 0 [:find $WANIP "/" -1]]; + +#-- From 05:00 to 05:15 hs, force the change to the fake ip address. +:if (($HOUR = 5) && ($MINU < 15)) do={ + :set WANIP $DNSFAK; + } + +#-- Loop through every hostname and URL. +:foreach XDOM,XURL in=$DNSALL do={ + #-- Delay. + :delay 1s; + + #-- Get the DNS resolution of the domain. + :local DNSIP ""; + :do { + :set DNSIP [:resolve $XDOM]; + } on-error={ + :set DNSIP "X"; + } + + #-- Check if no error and if the IP has changed. + :if (($DNSIP != "X") && ($DNSIP != $WANIP)) do={ + #-- IP changed. + do { + #-- Changes the dyndns resolution. + local RET [/tool fetch check-certificate="no" keep-result="no" url="$XURL$WANIP"]; + + #-- Log OK. + :log info ("ddns OK $XDOM: $DNSIP -> $WANIP"); + } on-error={ + #-- Log Error. + :log info ("ddns ER $XDOM: $DNSIP -> $WANIP"); + } + } + } + +#-- Finish. + + +### Raw script +--------------- + +/system script add name=DYNDNS policy=read,test source="\ + \n:local WANNAME \"WAN\";\ + \n:local DNSFAK \"127.0.0.1\";\ + \n:local DNSALL {\ + \n \".duckdns.org\"=\"https://www.duckdns.org/update\?domains=&token=&ip=\";\ + \n \".hopto.org\"=\"https://:@dynupdate.no-ip.com/nic/update\?hostname=.hopto.org&myip=\";\ + \n \".changeip.org\"=\"https://:@nic.changeip.com/nic/update\?hostname=.changeip.org&myip=\";\ + \n }\ + \n\ + \n:local HOUR [:tonum [:pick [/system clock get time] 0 2]];\ + \n:local MINU [:tonum [:pick [/system clock get time] 3 5]];\ + \n\ + \n:local WANIP [/ip address get [/ip address find interface=\$WANNAME disabled=no dynamic=yes] address];\ + \n:set WANIP [:pick \$WANIP 0 [:find \$WANIP \"/\" -1]];\ + \n\ + \n:if ((\$HOUR = 5) && (\$MINU < 15)) do={\ + \n :set WANIP \$DNSFAK;\ + \n }\ + \n\ + \n:foreach XDOM,XURL in=\$DNSALL do={\ + \n :delay 1s;\ + \n :local DNSIP \"\";\ + \n :do {\ + \n :set DNSIP [:resolve \$XDOM];\ + \n } on-error={\ + \n :set DNSIP \"X\";\ + \n }\ + \n :if ((\$DNSIP != \"X\") && (\$DNSIP != \$WANIP)) do={\ + \n do {\ + \n local RET [/tool fetch check-certificate=\"no\" keep-result=\"no\" url=\"\$XURL\$WANIP\"];\ + \n :log info (\"ddns OK \$XDOM: \$DNSIP -> \$WANIP\");\ + \n } on-error={\ + \n :log info (\"ddns ER \$XDOM: \$DNSIP -> \$WANIP\");\ + \n }\ + \n }\ + \n }\ + \n" + + +### End +-------- + +If you wants a new feature or found a bug, please open an issue in BitBucket: + + https://bitbucket.org/mangelo/snippets/src + + Thanks.