I have not yet had a chance to test this on a production router, but I believe that it should work. In order for the script to work, you need to have another default route on the router with a different routing mark, I have this route set up without any checks so that it will hopefully stay up at all times, and it sends the ping out of that route. The router will ping the supplied address 3 times, and if more than 2 fail, it will run again for a total of 3 times. It does this on 3 different supplied IP addresses, and if all 3 fail it will then run the specified operations. It will find any IP address, NAT rule, or route with the supplied comment, it must be the exact comment, but there can be multiple entries with that comment and it will enable/disable them all. Since the route that it is using to test the connection should remain online, once the scrip runs again and the internet connection is back online, it will then go back and undo all of the changes that it made.
Code: Select all
:local i 0; {:do {:set i ($i + 1)} while (($i < 3) && ([/ping 4.2.2.1 interval=2 routing-table=ISP1-Test count=3]<=1))};
:local x 0; {:do {:set x ($x + 1)} while (($x < 3) && ([/ping 4.2.2.2 interval=2 routing-table=ISP1-Test count=3]<=1))};
:local y 0; {:do {:set y ($y + 1)} while (($y < 3) && ([/ping 4.2.2.3 interval=2 routing-table=ISP1-Test count=3]<=1))};
:if ($i>=3 && $x>=3 && $y>=3) do={
:log info "ISP1 Down";
/ip address disable [find comment="ISP1 One-To-One NAT"];
/ip address enable [find comment="ISP2 One-To-One NAT"];
/ip firewall nat disable [find comment="ISP1 One-To-One NAT"];
/ip firewall nat enable [find comment="ISP2 One-To-One NAT"];
/ip route disable [find comment=ISP1];
} else { :log info "ISP1 UP";
/ip address enable [find comment="ISP1 One-To-One NAT"];
/ip address disable [find comment="ISP2 One-To-One NAT"];
/ip firewall nat enable [find comment="ISP1 One-To-One NAT"];
/ip firewall nat disable [find comment="ISP2 One-To-One NAT"];
/ip route enable [find comment=ISP1];
}
Edit: Modified it to remove the infinite loop. Also in order for this to work properly you need to disable the check-gateway function inside of "/ip routes". The reason for this is because it will disable any of the same route if one of them has check-gateway enabled.