I am trying to create a script for failover with multiple gateways (based on one I found) .
This command works:
:if ($pingresultA!=0) do={:foreach i in=[/ip route find gateway=$GatewayA] do={/ip route set $i disabled=no}}
This one works too (of course only when the result of find gateway=$GatewayA is one entry):
:if ([/ip route get [find gateway=$GatewayA] disabled]) do={/ip route set [find gateway=$GatewayA] disabled=no}
This is not working. What Am I doing wrong?
:if ($pingresultA!=0) do={:foreach i in=[/ip route find gateway=$GatewayA] do={:if ([/ip route get $i disabled]) do={/ip route set $i disabled=no}}
I want to enable only the disabled routes and not the already enabled ones in case the routes cut of momentarily from the reanabling. I read that in another script. This is the complete script that is working.
#set variables
:local pingcount 3
:local GatewayA 192.168.1.1
:local pingipA 69.16.137.252
:local GatewayB 192.168.2.254
:local pingipB 174.120.136.154
#:local GatewayC 192.168.3.254
#:local pingipC 174.120.136.155
:if ([/ip route print count-only where dst-address="$pingipA/32"]=0) do={/ip route add dst-address=($pingipA) gateway=($GatewayA) comment="Gateway $GatewayA Check"}
/ip route set [find dst-address="$pingipA/32"] disabled=no
:if ([/ip route print count-only where dst-address="$pingipB/32"]=0) do={/ip route add dst-address=($pingipB) gateway=($GatewayB) comment="Gateway $GatewayB Check"}
/ip route set [find dst-address="$pingipB/32"] disabled=no
#ping through gateways with static routes
:local pingresultA [/ping $pingipA count=$pingcount]
GatewayA enable/disable:
:if ($pingresultA=0) do={:foreach i in=[/ip route find gateway=$GatewayA] do={/ip route set $i disabled=yes}}
:if ($pingresultA!=0) do={:foreach i in=[/ip route find gateway=$GatewayA] do={/ip route set $i disabled=no}}
:local pingresultB [/ping $pingipB count=$pingcount]
GatewayB enable/disable:
:if ($pingresultB=0) do={:foreach i in=[/ip route find gateway=$GatewayB] do={/ip route set $i disabled=yes}}
:if ($pingresultB!=0) do={:foreach i in=[/ip route find gateway=$GatewayB] do={/ip route set $i disabled=no}}