Script for switching gateways

Hi all,
i have a Mikrotik router with several interfaces, both of them are gateways from different ISPs.

this is my /ip route pr:
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme,
B - blackhole, U - unreachable, P - prohibit

DST-ADDRESS PREF-SRC GATEWAY-STATE GATEWAY DISTANCE INTERFACE

0 S 0.0.0.0/0 reachable A.A.A.A 10 vlan1
1 A S 0.0.0.0/0 reachable B.B.B.B 1 vlan2

Both gateways are reachable all the time, one is primary - B.B.B.B with distance 1.
I`d like to make a script that is running all the time and watching for the states of the link and if gateway B.B.B.B goes down, the script should automatically put the gateway A.A.A.A as main gateway. An when the link with gateway B.B.B.B is up again the script should put it as a primary gateway.
I have manage to write a script but seems that it does not work, probably i am missing something.
Could someone check it and see if there is something wrong.

source=
/interface ethernet monitor vlan2
:set u $status
:while ($u = “link-ok”)
/ip route get [find dst-address=0.0.0.0/0] gateway]=B.B.B.B)
:if ($u != “link-ok”) do={ /ip route set 0 distance=1 }
&& /tool e-mail send to=“me@me.com” subject=“Gateway switched”

I`m using /ip route set distance=1 for the second gateway to set it as primary, sins the lower distance is the main default route.

Thank in advance

You shouldnt need a script for basic failover operations. Just enable “check-gateway=” on the route. If you choose ping it will ping the gateway and when it doesnt respond it will automatically enable the higher cost route. If you choose ARP it will look for MAC address in arp table, if its not there it will automatically fail over.

Sam

changeip,
thanks for the quick reply..

/ip route gateway=x.x.x.x check-gateway=ping is set up, but for some reason it does not work.
That is why i`m trying to write some simple script.

My question is how to go beyond the simple ping. I have 2 service providers (a DSL line and a fiber line) the intent is that if the fiber line fails, the DSL takes over. Here is my problem. When the fiber has failed in the past, the local router provided by the wholesaler still responds to pings, but there is nothing beyond that. Because it responds to pings, my RB will not failover, just assume that the line is active. I need to be able to have it ping beyond the gateway for connectivity. Any suggestions?

My question is how to go beyond the simple ping. I have 2 service providers (a DSL line and a fiber line) the intent is that if the fiber line fails, the DSL takes over. Here is my problem. When the fiber has failed in the past, the local router provided by the wholesaler still responds to pings, but there is nothing beyond that. Because it responds to pings, my RB will not failover, just assume that the line is active. I need to be able to have it ping beyond the gateway for connectivity. Any suggestions?

I have the same question. Does anybody have the answer yet?

Use netwatch to monitor an IP address further down the path, and fire scripts on up and down events that modify the configuration accordingly. You must be very careful to ensure that whatever you’re monitoring is only reachable through one path, usually that involves firewall filters dropping packets based on destination and out-interface unless routing makes it impossible for the second path to reach that target.

rarbolay, Tomasen, look at that page:

Advanced Routing Failover without Scripting