Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
bgabika authored Feb 26, 2023
0 parents commit ba5af44
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
95 changes: 95 additions & 0 deletions crx-scrpt_dyndns_dual-wan.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# COREX MIKROTIK DynDNS update script for dual WAN
# VERSION 1.0 - BUILT ON 2023.02.26.
#
# for more information check GitHub:
# https://github.com/bgabika/mikrotik_dyndns_for_dual_wan
#
# Test it in test environment to stay safe and sensible before
# using in production!
# For bugs and feature requests mailto bg@corex.bg
############################################################

:global IspDictionary ({"primary-ISP.getmyip.com"="digi"; \
"backup-ISP.getmyip.com"="ether1"})

:global ddnsuser "my_dyndns_user"
:global ddnspass "my_dyndns_token"

:global DynDnsFqdnPriority
:global IspInterfacePriority
:foreach mykey,myvalue in=$IspDictionary do={ :set $DynDnsFqdnPriority ($DynDnsFqdnPriority, $mykey); :set $IspInterfacePriority ($IspInterfacePriority, $myvalue) }

:global IspDistances ({})


:foreach interface in=$IspInterfacePriority \
do={ \
:global distance [ip route get [find where dst-address=0.0.0.0/0 and vrf-interface=$interface] distance ];
:set ($IspDistances->"$interface") $distance
}


:local interfacefunction \
do={ \
:global ddnsuser;
:global ddnspass;
:global IspInterfacePriority;
:global DynDnsFqdnPriority;
:global IspInterface [:pick $IspInterfacePriority $interfaceindex]; \
:global DynDnsFqdn [:pick $DynDnsFqdnPriority $fqdnindex]; \
:global IspIpActive [:resolve $DynDnsFqdn]; \
:global IspIpFresh [ /ip address get [/ip address find interface=$IspInterface ] address ]; \

:if ([ :typeof $IspIpFresh ] = nil ) \
do={
:log warning ("COREX-DNS: No ip address on $IspInterface .")
} \
else={
:for i from=( [:len $IspIpFresh] - 1) to=0 \
do={
:if ( [:pick $IspIpFresh $i] = "/") \
do={
:set IspIpFresh [:pick $IspIpFresh 0 $i];
}
}
}


:if ($IspIpActive != $IspIpFresh) \
do={
:log warning ("COREX-DNS: Old IP address = $IspIpActive")
:log warning ("COREX-DNS: Fresh IP address = $IspIpFresh")
:log warning "COREX-DNS: Update IP needed, Sending UPDATE...!"
:global str "/nic/update\?hostname=$DynDnsFqdn&myip=$IspIpFresh&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
/tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \
password=$ddnspass dst-path=("/DynDNS.".$DynDnsFqdn)
:delay 1
:global str [/file find name="DynDNS.$DynDnsFqdn"];
/file remove $str
:global IspIpActive $IspIpFresh
:log warning "COREX-DNS: $DynDnsFqdn IP updated to $IspIpFresh!"
} \
else={
:log warning "COREX-DNS: $DynDnsFqdn dont need changes";
}

}


if ([:pick $IspDistances 0] < [:pick $IspDistances 1]) \
do={ \
:log warning ("Primary ISP: ".[:pick $IspInterfacePriority 0])
$interfacefunction interfaceindex=0 fqdnindex=0 \
:delay 1
$interfacefunction interfaceindex=1 fqdnindex=1 \

} \

else={ \
:log warning ("Primary ISP: ".[:pick $IspInterfacePriority 1])
$interfacefunction interfaceindex=1 fqdnindex=0 \
:delay 1
$interfacefunction interfaceindex=0 fqdnindex=1 \

}

71 changes: 71 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

# crx-scrpt_dyndns_dual-wan

COREX MIKROTIK DynDNS update script for dual WAN, v1.0

### Features
- check Mikrotik multi ISP's interfaces and update DynDNS records.
- script is created to check mikrotik device with dual WAN interface and for router access on the backup interface but from main fqdn.
- script is working on router OS 7.7 and above.
- IP address update procedure is based on interface distance.
- Example to understand backend operation:
:global IspDictionary ({"primary-ISP.getmyip.com"="digi"; \
"backup-ISP.getmyip.com"="ether1"})

Router can be accessed on primary-ISP.getmyip.com fqdn on the interface named "digi" as primary ISP.
Router has a backup ISP on backup-ISP.getmyip.com fqdn on the interface named "ether1".
In this case "digi" interface has lower distance then "ether1" interface.

Script will check "digi" interface's ip address and will update primary-ISP.getmyip.com address in your DynDNS account.

When "digi" interface is down, backup ISP's "ether1' interface will be active on the router.
In this case the script will update my-primary.getmyip.com fqdn with "ether1" ip address, so your router can be accessed on the backup ISP's interface on the main fqdn.

IMPORTANT!!!!
This script does not change any value in your router.
This script gets IP address information from router interfaces only and put them into dyndns account.
Active and backup ISP changes must be solved from other source.


### Usage

<pre><code>
# USAGE:
# - edit crx-scrpt_dyndns_dual-wan.rsc file: ddnsuser and ddnspass for DynDNS connection
# edit IspDictionary dictionary, first key is the primary FQDN, first value is the primary ISP's interface name.
# Second key and value are the backup ISP's details.
# - copy modified crx-scrpt_dyndns_dual-wan.rsc file on the MIKROTIK device.
# - Create task scheduler to trigger the script in every 5 minutes:
# /system scheduler add interval=00:05:00 name=COREX_DynDNS_update on-event="import crx-scrpt_dyndns_dual-wan.rsc"
# - For output check mikrotik log, Example output:
# Primary ISP: digi
# COREX-DNS: primary-ISP.getmyip.com dont need changes
# COREX-DNS: backup-ISP.getmyip.com dont need changes
# ...
# Primary ISP: ether1-NET
# COREX-DNS: Old IP address = 44.119.19.71
# COREX-DNS: Fresh IP address = 192.168.30.118
# COREX-DNS: Update IP needed, Sending UPDATE...!
# COREX-DNS: primary-ISP.getmyip.com IP updated to 192.168.30.118!
# ...
# COREX-DNS: Old IP address = 88.133.199.155
# COREX-DNS: Fresh IP address = 192.168.31.109
# COREX-DNS: Update IP needed, Sending UPDATE...!
# COREX-DNS: backup-ISP.getmyip.com IP updated to 192.168.31.109!

</code></pre>



### Version

- v1.0

### ToDo

- waiting for bugs or feature requests (-:

## Changelog

- [initial release] version 1.0

0 comments on commit ba5af44

Please sign in to comment.