Mayby someone here can help with the syntax.
I would like the script to check if the state is already in “failover”, by looking at the route with the comment “Secondary” and checking if has a distance value of 10 and if it has that, add the Primary route with a distance value of 20 and if not, just add it with a distance value of 10.
This is important, so that if the Primary WAN interface flaps, it does not come back with a distance of 10, otherwise one would end up with two routes with a distance of 10, one thats alive and one that is not.
I will post the full example of my Dual DHCP wan setup with failover setup, once we have this working.
{
:local failstate [:local count [/ip route print count-only where comment="Secondary" and distance=10]
:if ($failstate=1) do={
{
:local count [/ip route print count-only where comment="Primary"]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="Primary" distance=20
} else={
:if ($count = 1) do={
:local test [/ip route find where comment="Primary"]
:if ([/ip route get $test gateway] != $"gateway-address") do={
/ip route set $test gateway=$"gateway-address"
}
} else={
:error "Multiple routes found"
}
}
} else={
/ip route remove [find comment="Primary"]
}
}
} else={
:local count [/ip route print count-only where comment="Primary"]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="Primary" distance=10
} else={
:if ($count = 1) do={
:local test [/ip route find where comment="Primary"]
:if ([/ip route get $test gateway] != $"gateway-address") do={
/ip route set $test gateway=$"gateway-address"
}
} else={
:error "Multiple routes found"
}
}
} else={
/ip route remove [find comment="Primary"]
}
}