The setup works. Notice how the scope is used to recursively lookup routes.
I personally dont like this, because it makes a mess of your routing table. You can look at my failover script on the wiki http://wiki.mikrotik.com/wiki/Failover_Scripting
It monitors an IP, if that goes down, it increases the distance of the route that is down. This will give your other default route higher priority, by which, you achieve failover.
My script relies on mangle already being properly configured by you. It only works with routing tables.
These lines do the checking.
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP1]
:set PingResult [ping $PingTarget count=1 interface=$InterfaceISP2]
If the ping is not replied back a configured number of times, all routes that live on that interface have their distance increased by what you configure.
So when ISP1 is up, whole network goes through ISP1. Mangle is configured so connectivity which comes from WAN is replied to correctly in each routing table (standard mangle with no load balancing for 2 ISPs)
So the script pings and checks both ISPs. If ISP1 goes down the script increases routes which use its gateway by a certain distance. (lets say 20) So in main routing table, ISP2 now has lower distance, therefore whole network uses ISP2. This achieves failover.
I have adjusted your script accordingly. Will I need to make double entries in the ip firewall filter and ip firewall nat for both WAN’s I assume. my drops are currently set to 10 drops, in the scheduler do you know the time you set for the script to run?
Your main routing table has 2 routes with distance 1, one route needs to have higher distance. Also, if that is all you have in your mangle, you need much more. Feel free to watch the video in my sig for more info on proper 2 WAN mangle. With your mangle, you will not be able to reach the router properly from the outside from both WANs.
The scheduler is up to you. I usually use 10 seconds with 4 failures. For firewall, that all depends on how you have it setup. If you only use input and forward chains, then all is taken care of. If you firewall based on incoming interface and jump to custom chains, then you need to jump from both interfaces.
So what is the problem with the NAT? Simply make sure you are NATing based on an “WAN_IP” address list, which contains both your WAN IP addresses. Do not NAT based on incoming interface, or you will have to have all your NAT rules multiple times.
For src-nat you will have to have 2 src NAT rules for each server unfortunatelly. Or you can just masq both outgoing interfaces and all servers will share a single IP for outgoing traffic only. Incoming traffic, thanks to dst-nat rules will be replied with correct IP however. In case you want src-nats for each server, do it like this:
# ------------- start editing here -------------
# Edit the variables below to suit your needs
# Please fill the WAN interface names
:local InterfaceISP1 ether1
:local InterfaceISP2 ether2
# Please fill the gateway IPs (or interface names in case of PPP)
:local GatewayISP1 68.*.*.1
:local GatewayISP2 50.*.*.1
# Please fill the ping check host - currently: google public dns
:local PingTarget 8.8.8.8
# Please fill how many ping failures are allowed before fail-over happens
:local FailTreshold 3
# Define the distance increase of a route when it fails
:local DistanceIncrease 2
ip route print:
0 A S dst-address=0.0.0.0/0 gateway=68.*.*.1
gateway-status=68.*.*.1 reachable via ether1 distance=1 scope=30
target-scope=10 routing-mark=sv
1 A S dst-address=0.0.0.0/0 gateway=50.*.*.1
gateway-status=50.*.*.1 reachable via ether2 distance=1
scope=30 target-scope=10 routing-mark=cc
2 A S dst-address=0.0.0.0/0 gateway=68.*.*.1
gateway-status=68.*.*.1 reachable via ether1 distance=1 scope=30
target-scope=10
3 S dst-address=0.0.0.0/0 gateway=50.*.*.1
gateway-status=50.*.*.1 reachable via ether2 distance=2
scope=30 target-scope=10
When I look @ the Environment tab in Script List, I can see PingFailCountISP2 and 1. If I unplug ISP1 it now is not failing over to ISP2 and when I plug ISP1 back in it does not faill back to ISP1.