Let's say I've got two WAN ports (ether1 and ether2), first one is main WAN port, second is for backup. The thing is I don't want (or rather can't have) load balancing between two networks, so I'm forcing 8.8.8.8 to go through ether1, remaining traffic to go throught ether1 (higher distance) and ether2 just sits there and waits (accepting incoming traffic).
Code: Select all
/ip route
add comment=CHECK disabled=no distance=1 dst-address=8.8.8.8/32 gateway=192.168.1.1
add comment=MAIN disabled=no distance=2 dst-address=0.0.0.0/0 gateway=192.168.1.1
add comment=BACKUP disabled=no distance=3 dst-address=0.0.0.0/0 gateway=192.168.9.1
Code: Select all
/ip dhcp-client
add add-default-route=no comment=defconf interface=ether1 script="/ip route set [find comment=\"CHECK\"] gateway=([/ip dhcp-client get [find interface\
=ether1]]->\"gateway\")\r\
\n/ip route set [find comment=\"MAIN\"] gateway=([/ip dhcp-client get [find interface=ether1]]->\"gateway\")"
add add-default-route=no comment=defconf interface=ether2 script=\
"/ip route set [find comment=\"BACKUP\"] gateway=([/ip dhcp-client get [find interface=ether2]]->\"gateway\")"
Code: Select all
/system script
add dont-require-permissions=no name=wan_failover owner=admin policy=read,write,policy,test source="#SCRIPT SOURCE BELOW FOR BETTER READABILLITY"
Code: Select all
:local PingFailTreshold 5
:global PingFailCount
:if ([:typeof $PingFailCount] = "nothing") do={:set $PingFailCount 0}
:local PingResult
:set $PingResult [:typeof ([/tool/ping address=8.8.8.8 count=1 interval=1 as-value]->"status")]
:if ($PingResult = "nothing") do={
:if ($PingFailCount > 0) do={
:if ($PingFailCount >= ($PingFailTreshold -1)) do={
/log/error "WAN on ether1 is UP, adjusting routes"
/ip/route/set [find comment="MAIN"] distance=2
}
:set $PingFailCount 0
}
} else={
:set $PingFailCount ($PingFailCount + 1)
:if ($PingFailCount = ($PingFailTreshold -1)) do={
/log/error "WAN on ether1 is DOWN, adjusting routes"
/ip/route/set [find comment="MAIN"] distance=10
}
}
Code: Select all
/system scheduler
add interval=5s name=wan_failover on-event=wan_failover policy=read,write,policy,test start-time=startup
So, please tell me, am I crazy, or is this REALLY SIMPLE solution actually works?