Failover script for static Ethernet WAN IP and USB 3G

Hi,

Can anybody publicate Mikrotik failover script for such configuration:

Connection to Internet provider 1 have static IP over Ethernet (interface name “ether1-gateway”)
Connection to Internet provider 2 have DHCP IP over USB 3G (interface name “ppp-out1”)

Internet connection to Provider 2 must start when Provider 1 down, and return when Provider 1 again online.

I think that this should help you:
http://wiki.mikrotik.com/wiki/Two_gateways_failover

Simply have two routes with different distance parameters and add check-gateway=ping option for primary one.

Thanks, testing this variant. Some problems occurs - how clear established connection from old provider after switching to new provider?

Is it possible to turn on ppp-connection (3G interface) when main provider down only and then turn off 3G when main provider work normally?

Is it possible check ping other 1-3 IP address, not provider gateway IP ?

The wiki scripts for failover don’t work. Been discussed multiple times. The other main issue is you have NAT rules that are based off say ETH1 and even if mikrotik switches over the rule then needs to be modified or disabled and a second rule enabled to change the NAT over to WLAN1. I’ve had to create two scripts that I can run that go thru a gantlet of adjustments to switch between and haven’t figured out how to automate the failover. I’ve disabling old gateway, enable new gateway, disable org eth interface, enabling wlan interface, disabling org NAT, enabling WLAN NAT. I use a switch back script to reverse those adjustment.

Hello

This code from MikroTik config generator
If you specify a USB modem, is created and the script to switch to the modem.Though there is all in Russian, but nothing complicated.

/ip route add dst-address=8.8.4.4 gateway=127.0.0.1 distance=1
/system scheduler add disabled=no interval=30s name=CheckInet on-event=BackupModem
/system script add name=BackupModem owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#Name: BackupModem\r\
\n#Version: 3.4\r\
\n\r\
\n:local debug false;\r\
\n\r\
\n:local ShutdownModem false;\r\
\n\r\
\n:local UseModem false;\r\
\n\r\
\n:local OldGW [/ip route get [find dst-address=8.8.4.4/32] gateway];\r\
\n:local NewGW [/ip dhcp-client get [/interface find name=wan] gateway];\r\
\n:if (\$OldGW!=\$NewGW) do={\r\
\n\t/ip route set [find dst-address=8.8.4.4/32] gateway=\$NewGW;\r\
\n\t:log info \"BackupModem: Change gateway for 8.8.4.4 from \$OldGW to \$NewGW\";\r\
\n} else {\r\
\n\t:if (\$debug) do={:log info \"BackupModem: GW for 8.8.4.4 not change \$OldGW/\$NewGW\";}\r\
\n}\r\
\n\r\
\n:local WANstatus [/interface get wan running];\r\
\n:if (\$WANstatus=false) do={\r\
\n\t:set UseModem true;\r\
\n\t:if (\$debug) do={:log info \"BackupModem: wan link Down, must use Modem.\";}\r\
\n} else {\r\
\n\t:if (\$debug) do={:log info \"BackupModem: wan link OK.\";}\r\
\n\t:local CheckMainISP [/ping 8.8.4.4 count=3 interface=wan];\r\
\n\t:if (\$CheckMainISP=3) do={\r\
\n\t\t:set UseModem false;\r\
\n\t\t:if (\$debug) do={:log info \"BackupModem: Ping Ok, Not use USB modem\";}\r\
\n\t} else {\r\
\n\t\t:set UseModem true;\r\
\n\t\t:if (\$debug) do={:log info \"BackupModem: Ping lost, Must use USB modem\";}\r\
\n\t}\r\
\n}\r\
\n:local Distance [/ip dhcp-client get [/interface find name=\"wan\"] default-route-distance];\r\
\n:if (\$Distance=1) do={\r\
\n\t:if (\$debug) do={:log info \"BackupModem: Distance is 1\";}\r\
\n\t:if (\$UseModem=true) do={\r\
\n\t\t:local ModemStatus [/interface get [/interface find name=\"wan-4G\"] running];\r\
\n\t\t:if (\$ModemStatus=false) do={\r\
\n\t\t\t/interface enable [find name=\"wan-4G\"];\r\
\n\t\t\t:delay 5s;\r\
\n\t\t\t:local ModemStatus [/interface get [/interface find name=\"wan-4G\"] running];\r\
\n\t\t}\r\
\n\t\t:if (\$ModemStatus=false) do={\r\
\n\t\t\t:log warning \"BackupModem: USB modem not ready\";\r\
\n\t\t} else {\r\
\n\t\t\t:if (\$debug) do={:log info \"BackupModem: USB modem ready.\";}\r\
\n\t\t\t/ip dhcp-client set [/interface find name=\"wan\"] default-route-distance=3;\r\
\n\t\t\t:log info \"BackupModem: use USB modem as backup.\";\r\
\n\t\t}\r\
\n\t}\r\
\n} else {\r\
\n\t:if (\$debug) do={:log info \"BackupModem: Distance is NOT 1\";}\r\
\n\t:if (\$UseModem=false) do={\r\
\n\t\t:log info \"BackupModem: Use main ISP.\";\r\
\n\t\t/ip dhcp-client set [/interface find name=\"wan\"] default-route-distance=1;\r\
\n\t\t:if (\$ShutdownModem) do={/interface disable [find name=\"wan-4G\"]};\r\
\n\t} else {\r\
\n\t\t:if (\$debug) do={:log info \"BackupModem: already use USB modem.\";}\r\
\n\t}\r\
\n}\r\n"