If it’s all the same to you, you can also achieve this by setting routing distances for the default routes on both devices. As soon as the default route with the lowest distance will become unavailable (check gateway) it’ll switch to next in line.
I know, but the second Gateway is a 3G stick and should only exchange data in the worst case. That’s the reason, why I am looking for the way by script. But if you have a possibility to do this without any script, please explain. Thx.
Assuming you’re paying for data and not for uptime the routing distances should suffice (as - bar a keepalive - no data should be transferred so long a lower weighted route is active).
Several issues with your script:
-both cases are doing the same thing (i.e. it is enabling whether ping is succesful and not); as you’r evaluating negatively (!=) change the ‘do’ case to disable.
-the syntax of the ping evaluation should be like :if ([/ping … ] != 0)
-could clean-up some unnecessary variables (e.g. gateways, pingresult)
Fixing above issues should make the script functional.
Use the scheduler to run the script on the interval of your choosing.
Thanks for the recommendation and hints for the script. I would try again the solution by routing. Can you refer to an working example? Do I need to remove the automatically generated routings? Do I only need to configure the routings or is there something else?
Depends how you populate your routing table; if you’re using static routes you can modify the distance in the /ip routing entries. If you’re getting your default routes from the DHCP client or PPP you configure the distance at the designated DHCP or PPP entry. In any case just set a higher distance for the backup WAN compared to your main WAN.
Please provide the relevant portion of /ip route print if you’re stuck.