Mikrotik 3-WAN Failover


Recently i have come across a problem with my Mikrotik Router. I have 3 WAN Interfaces with a known Gateway (which is an intermediate Gateway) and since i do not know if i ping that, if it currently has Internet Access, i thought i would like to check if it has. Router OS has a feature built in called Detect-Internet which categorizes your Interfaces into lists, with which you can categorize if your Interface has Internet or if it has WAN or only LAN. Sadly you can not use it yet(!) to use the check-gateway option in the routes. But the nice thing about Router OS is that you can use scripting. For that purpose I have created a script which gets my route based on comments and sets the metric (Distance) based on the Lists.

# Detect-Internet gesteuertes Failover: parkt/entparkt WAN-Default-Routen per Distance
# Konfiguration:
:local aliases {"wan1";"wan2";"wan3"}
:local ifaces  {"bridge-lp-wan";"bridge-hq-lan";"vlan3-jwremote"}
:local dists   {10;20;30}
:local parkDist 250
# Name der von Detect-Internet befüllten Interface-Liste:
:local diListName "internet-detected"

# Logik:
:for i from=0 to=2 do={
    :local alias [:pick $aliases $i]
    :local iface [:pick $ifaces  $i]
    :local dist  [:pick $dists   $i]
    :local tag ("WAN:" . $alias)

    :local inList ([:len [/interface/list/member find list=$diListName interface=$iface]] > 0)

    :if ($inList) do={
        /ip route enable [find comment=$tag]
        /ip route set [find comment=$tag] distance=$dist
    } else={
        /ip route set [find comment=$tag] distance=$parkDist
    }
}

It parks the route if it can not reach the internet on this interface with a metric of 250. Otherwise it uses the metric defined for the interface. The variables are the following:

  • aliases
    • is for the routes to know which route defines which interface (example: wan1 defines the default route of interface bridge-lp-wan )
  • ifaces:
    • this defines the interfaces which have the aliases bind to them (see above)
  • dists:
    • defines the metric of each route in the normal connected state
  • parkDist:
    • defines the metric of each route in the non-internet/wan-only/lan-only state
  • diList:
    • this is the name of the list, which is selected under the “Interface Internet List” in the Detect Internet Configuration of the Router

To run the Script, you should use a scheduler in Router OS.

Hope that helps if you run into a similar problem


Leave a Reply

Your email address will not be published. Required fields are marked *