Update dyndns with Mikrotik.

This commit is contained in:
Miguel Scapolla 2018-03-29 15:29:13 -03:00
parent c69904f24e
commit 3004d35320

View File

@ -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 {
"<SUBDOMAIN>.duckdns.org"="https://www.duckdns.org/update?domains=<SUBDOMAIN>&token=<TOKEN>&ip=";
"<SUBDOMAIN>.hopto.org"="https://<USERNAME>:<PASSWORD>@dynupdate.no-ip.com/nic/update?hostname=<SUBDOMAIN>.hopto.org&myip=";
"<SUBDOMAIN>.changeip.org"="https://<USERNAME>:<PASSWORD>@nic.changeip.com/nic/update?hostname=<SUBDOMAIN>.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 \"<SUBDOMAIN>.duckdns.org\"=\"https://www.duckdns.org/update\?domains=<SUBDOMAIN>&token=<TOKEN>&ip=\";\
\n \"<SUBDOMAIN>.hopto.org\"=\"https://<USERNAME>:<PASSWORD>@dynupdate.no-ip.com/nic/update\?hostname=<SUBDOMAIN>.hopto.org&myip=\";\
\n \"<SUBDOMAIN>.changeip.org\"=\"https://<USERNAME>:<PASSWORD>@nic.changeip.com/nic/update\?hostname=<SUBDOMAIN>.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.