/ip firewall mangle
add action=mark-connection chain=input in-interface=pppoe-out1 \
new-connection-mark=From-ISP-1
add action=mark-routing chain=output connection-mark=From-ISP-1 \
new-routing-mark=To-ISP-1
add action=mark-connection chain=input in-interface=ether2 \
new-connection-mark=From-ISP-2
add action=mark-routing chain=output connection-mark=From-ISP-2 \
new-routing-mark=To-ISP-2
/ip route
add distance=1 gateway=pppoe-out1 routing-mark=To-ISP-1
add distance=1 gateway=109.87.159.126 routing-mark=To-ISP-2
add comment=ISP-1 distance=1 gateway=pppoe-out1
add comment=ISP-2 distance=2 gateway=109.87.159.126
/ip route
add comment=Netwatch distance=1 dst-address=8.8.8.8/32 gateway=pppoe-out1
/ip firewall filter
add action=drop chain=output comment=Netwatch dst-address=8.8.8.8 \
out-interface=ether2 protocol=icmp
/tool netwatch
add down-script="/ip route disable [find comment=\"ISP-1\"]\r\
\n/ip route enable [find comment=\"ISP-2\"]\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 (\"ISP-1 IS DOWN\")" host=8.8.8.8 up-script="/ip route disab\
le [find comment=\"ISP-2\"]\r\
\n/ip route enable [find comment=\"ISP-1\"]\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 (\"ISP-1 IS UP\")"
/ip firewall address-list
add address=192.168.2.2-192.168.2.125 list=To-ISP-1
add address=192.168.2.126-192.168.2.254 list=To-ISP-2
/ip firewall mangle
add action=mark-connection chain=prerouting disabled=no dst-address-type=\
!local new-connection-mark=To-ISP-1-Connection src-address-list=To-ISP-1
add action=mark-routing chain=prerouting connection-mark=To-ISP-1-Connection \
disabled=no new-routing-mark=To-ISP-1 src-address-list=To-ISP-1
add action=mark-connection chain=prerouting disabled=no dst-address-type=\
!local new-connection-mark=To-ISP-2-Connection src-address-list=To-ISP-2
add action=mark-routing chain=prerouting connection-mark=To-ISP-2-Connection \
disabled=no new-routing-mark=To-ISP-2 src-address-list=To-ISP-2
/ip route
add comment=Group-1-1 distance=1 gateway=pppoe-out1 routing-mark=To-ISP-1
add comment=Group-2-1 disabled=yes distance=2 gateway=109.87.159.126 \
routing-mark=To-ISP-1
add comment=Group-2-2 distance=1 gateway=109.87.159.126 routing-mark=To-ISP-2
add comment=Group-1-2 disabled=yes distance=2 gateway=pppoe-out1 \
routing-mark=To-ISP-2
add comment=Netwatch-2 distance=1 dst-address=8.8.4.4/32 gateway=109.87.159.126
add comment=Netwatch-1 distance=1 dst-address=8.8.8.8/32 gateway=\
pppoe-out1
/ip firewall filter
add action=drop chain=output comment=Netwatch-1 dst-address=8.8.8.8 \
out-interface=ether2 protocol=icmp
add action=drop chain=output comment=Netwatch-2 dst-address=8.8.4.4 \
out-interface=!ether2 protocol=icmp
/tool netwatch
add down-script="/ip route disable [find comment=\"ISP-2\"]\r\
\n/ip route disable [find comment=\"Group-2-2\"]\r\
\n/ip route enable [find comment=\"Group-1-2\"]\r\
\nlog warning (\"ISP-2 IS DOWN\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"To-ISP-2-C\
onnection\"] do={ /ip firewall connection remove \$i }" host=8.8.4.4 \
up-script="/ip route enable [find comment=\"ISP-2\"]\r\
\n/ip route enable [find comment=\"Group-2-2\"]\r\
\n/ip route disable [find comment=\"Group-1-2\"]\r\
\nlog warning (\"ISP-2 IS UP\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"To-ISP-2-C\
onnection\"] do={ /ip firewall connection remove \$i }"
add down-script="/ip route disable [find comment=\"ISP-1\"]\r\
\n/ip route disable [find comment=\"Group-1-1\"]\r\
\n/ip route enable [find comment=\"Group-2-1\"]\r\
\nlog warning (\"ISP-1 IS DOWN\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"To-ISP-1-C\
onnection\"] do={ /ip firewall connection remove \$i }" host=8.8.8.8 \
up-script="/ip route enable [find comment=\"ISP-1\"]\r\
\n/ip route enable [find comment=\"Group-1-1\"]\r\
\n/ip route disable [find comment=\"Group-2-1\"]\r\
\nlog warning (\"ISP-1 IS UP\")\r\
\n:foreach i in=[/ip firewall connection find connection-mark~\"To-ISP-1-C\
onnection\"] do={ /ip firewall connection remove \$i }"
/ip firewall mangle
add action=mark-connection chain=prerouting disabled=yes dst-address-type=\
!local new-connection-mark=To-ISP-1-Connection src-address-list=To-ISP-1 nth=2,1
add action=mark-routing chain=prerouting connection-mark=To-ISP-1-Connection \
disabled=yes new-routing-mark=To-ISP-1 src-address-list=To-ISP-1
add action=mark-connection chain=prerouting disabled=yes dst-address-type=\
!local new-connection-mark=To-ISP-2-Connection src-address-list=To-ISP-2 nth=2,2
add action=mark-routing chain=prerouting connection-mark=To-ISP-2-Connection \
disabled=yes new-routing-mark=To-ISP-2 src-address-list=To-ISP-2
/ip firewall mangle
add action=mark-connection chain=prerouting dst-address=\
!192.168.2.0/24 new-connection-mark=To-ISP-1-Connection \
per-connection-classifier=both-addresses-and-ports:2/0 src-address=\
192.168.2.0/24
add action=mark-routing chain=prerouting connection-mark=To-ISP-1-Connection \
new-routing-mark=To-ISP-1 src-address=192.168.2.0/24
add action=mark-connection chain=prerouting dst-address=\
!192.168.2.0/24 new-connection-mark=To-ISP-2-Connection \
per-connection-classifier=both-addresses-and-ports:2/1 src-address=\
192.168.2.0/24
add action=mark-routing chain=prerouting connection-mark=To-ISP-2-Connection \
new-routing-mark=To-ISP-2 src-address=192.168.2.0/24
{
:local rmark "ISP-2"
:local count [/ip route print count-only where comment="ISP-2"]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="ISP-2" distance=2
} else={
:if ($count = 1) do={
:local test [/ip route find where comment="ISP-2"]
: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="ISP-2"]
}
}
{
:local rmark "ISP-1"
:local count [/ip route print count-only where comment="ISP-1"]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="ISP-1" 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="ISP-1"]
: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="ISP-1"]
/ip route remove [find comment="Netwatch"]
}
}
/ip firewall mangle
add chain=forward action=mark-connection new-connection-mark=From-ISP-1 in-interface=pppoe-out1
add chain=prerouting action=mark-routing new-routing-mark=To-ISP-1 passthrough=no connection-mark=From-ISP-1 in-interface=bridge
add chain=forward action=mark-connection new-connection-mark=From-ISP-2 in-interface=ether2
add chain=prerouting action=mark-routing new-routing-mark=To-ISP-2 passthrough=no connection-mark=From-ISP-2 in-interface=bridge