/ip firewall mangle
add action=mark-connection chain=input in-interface=ether1-wan1 \
new-connection-mark=From-ISP1
add action=mark-routing chain=output connection-mark=From-ISP1 \
new-routing-mark=ISP1
add action=mark-connection chain=input in-interface=ether2-wan2 \
new-connection-mark=From-ISP2
add action=mark-routing chain=output connection-mark=From-ISP2 \
new-routing-mark=ISP2
/ip route
add distance=1 gateway=1.1.1.2 routing-mark=ISP1
add distance=1 gateway=2.2.2.2 routing-mark=ISP2
add comment=ISP1 distance=1 gateway=1.1.1.2
add comment=ISP2 distance=2 gateway=2.2.2.2
/ip route
add comment=Netwatch distance=1 dst-address=8.8.8.8/32 gateway=1.1.1.2
/ip firewall filter
add action=drop chain=output comment=Netwatch dst-address=8.8.8.8 \
out-interface=ether2-wan2 protocol=icmp
/tool netwatch
add down-script="/ip route disable [find comment=\"ISP1\"]\r\
\n/ip route enable [find comment=\"ISP2\"]\r\
\n:foreach i in=[/ip firewall connection find protocol~\"tcp\"] do={ /ip f\
irewall connection remove \$i }\r\
\n:foreach i in=[/ip firewall connection find protocol~\"udp\"] do={ /ip f\
irewall connection remove \$i }\r\
\nlog warning (\"ISP1 IS DOWN\")" host=8.8.8.8 up-script="/ip route disab\
le [find comment=\"ISP2\"]\r\
\n/ip route enable [find comment=\"ISP1\"]\r\
\n:foreach i in=[/ip firewall connection find protocol~\"tcp\"] do={ /ip f\
irewall connection remove \$i }\r\
\n:foreach i in=[/ip firewall connection find protocol~\"udp\"] do={ /ip f\
irewall connection remove \$i }\r\
\nlog warning (\"ISP1 IS UP\")"
/ip firewall address-list
add address=192.168.1.2-192.168.1.125 list=ISP1
add address=192.168.1.126-192.168.1.254 list=ISP2
/ip firewall mangle
add action=mark-connection chain=prerouting disabled=no dst-address-type=\
!local new-connection-mark=ISP1 src-address-list=ISP1
add action=mark-routing chain=prerouting connection-mark=ISP1 \
disabled=no new-routing-mark=ISP1 src-address-list=ISP1
add action=mark-connection chain=prerouting disabled=no dst-address-type=\
!local new-connection-mark=ISP2 src-address-list=ISP2
add action=mark-routing chain=prerouting connection-mark=ISP2 \
disabled=no new-routing-mark=ISP2 src-address-list=ISP2
/ip route
add comment=Group-1-1 distance=1 gateway=1.1.1.2 routing-table=ISP1
add comment=Group-2-1 disabled=yes distance=2 gateway=2.2.2.2 \
routing-table=ISP1
add comment=Group-2-2 distance=1 gateway=2.2.2.2 routing-table=ISP2
add comment=Group-1-2 disabled=yes distance=2 gateway=1.1.1.2 \
routing-table=ISP2
add comment=Netwatch-2 distance=1 dst-address=8.8.4.4/32 gateway=2.2.2.2
add comment=Netwatch-1 distance=1 dst-address=8.8.8.8/32 gateway=\
1.1.1.2
/ip firewall filter
add action=drop chain=output comment=Netwatch-1 dst-address=8.8.8.8 \
out-interface=ether2-wan2 protocol=icmp
add action=drop chain=output comment=Netwatch-2 dst-address=8.8.4.4 \
out-interface=!ether2-wan2 protocol=icmp
/tool netwatch
add down-script="/ip route disable [find comment=\"ISP2\"]\r\
\n/ip route disable [find comment=\"Group-2-2\"]\r\
\n/ip route enable [find comment=\"Group-1-2\"]\r\
\nlog warning (\"ISP2 IS DOWN\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"ISP2\"] do={ /ip firewall connection remove \$i }" host=8.8.4.4 \
up-script="/ip route enable [find comment=\"ISP2\"]\r\
\n/ip route enable [find comment=\"Group-2-2\"]\r\
\n/ip route disable [find comment=\"Group-1-2\"]\r\
\nlog warning (\"ISP2 IS UP\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"ISP2\"] do={ /ip firewall connection remove \$i }"
add down-script="/ip route disable [find comment=\"ISP1\"]\r\
\n/ip route disable [find comment=\"Group-1-1\"]\r\
\n/ip route enable [find comment=\"Group-2-1\"]\r\
\nlog warning (\"ISP1 IS DOWN\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"ISP1\"] do={ /ip firewall connection remove \$i }" host=8.8.8.8 \
up-script="/ip route enable [find comment=\"ISP1\"]\r\
\n/ip route enable [find comment=\"Group-1-1\"]\r\
\n/ip route disable [find comment=\"Group-2-1\"]\r\
\nlog warning (\"ISP1 IS UP\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"ISP1\"] do={ /ip firewall connection remove \$i }" /ip firewall mangle
add action=mark-connection chain=prerouting disabled=yes dst-address-type=\
!local new-connection-mark=ISP1 src-address-list=ISP1 nth=2,1
add action=mark-routing chain=prerouting connection-mark=ISP1 \
disabled=yes new-routing-mark=ISP1 src-address-list=ISP1
add action=mark-connection chain=prerouting disabled=yes dst-address-type=\
!local new-connection-mark=ISP2 src-address-list=ISP2 nth=2,2
add action=mark-routing chain=prerouting connection-mark=ISP2 \
disabled=yes new-routing-mark=ISP2 src-address-list=ISP2 /ip firewall mangle
add action=mark-connection chain=prerouting dst-address=\
!192.168.1.0/24 new-connection-mark=ISP1 \
per-connection-classifier=both-addresses-and-ports:2/0 src-address=\
192.168.1.0/24
add action=mark-routing chain=prerouting connection-mark=ISP1 \
new-routing-mark=ISP1 src-address=192.168.1.0/24
add action=mark-connection chain=prerouting dst-address=\
!192.168.1.0/24 new-connection-mark=ISP2 \
per-connection-classifier=both-addresses-and-ports:2/1 src-address=\
192.168.1.0/24
add action=mark-routing chain=prerouting connection-mark=ISP2 \
new-routing-mark=ISP2 src-address=192.168.1.0/24
{
:local rmark "ISP2"
:local count [/ip route print count-only where comment="ISP2"]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="ISP2" distance=2
} else={
:if ($count = 1) do={
:local test [/ip route find where comment="ISP2"]
: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="ISP2"]
}
} {
:local rmark "ISP1"
:local count [/ip route print count-only where comment="ISP1"]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="ISP1" distance=1
/ip route add gateway=$"gateway-address" comment="Netwatch" distance=1 dst-address=8.8.4.4
} else={
:if ($count = 1) do={
:local test [/ip route find where comment="ISP1"]
: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="ISP1"]
/ip route remove [find comment="Netwatch"]
}
} /ip firewall mangle
add chain=forward action=mark-connection new-connection-mark=From-ISP-1 in-interface=ether1-wan1
add chain=prerouting action=mark-routing new-routing-mark=ISP1 passthrough=no connection-mark=From-ISP-1 in-interface=bridge-lan
add chain=forward action=mark-connection new-connection-mark=From-ISP-2 in-interface=ether2-wan2
add chain=prerouting action=mark-routing new-routing-mark=ISP2 passthrough=no connection-mark=From-ISP-2 in-interface=bridge-lan