Automatic switching between redundant lines on MikroTik

Published: July 6, 2022

Last Modified: March 5, 2023

Automatic switching between redundant lines on MikroTik (RouterOS)

Suppose there are two Internet channels, from different or one provider, the gateway may be the same, it does not matter. They are included in the first and second ports.

Let’s add scripts with the names “ISP1DOWN” and “ISP2DOWN” that will ping some host, for example, the Google DNS address 8.8.4.4 is used, if it becomes unavailable, then the port with the active channel will be turned off, and the secondary one will turn on.
An example of adding a script through the terminal:

  • system script add name=ISP1DOWN policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=”script text”


ISP1DOWN script:

  • :if ([/ping 8.8.4.4 count=5] = 0) do={/interface disable ether1 ;/interface enable ether2 ;/system scheduler disable ISP1 ;/system scheduler enable ISP2}

ISP2DOWN script:

  • :if ([/ping 8.8.4.4 count=5] = 0) do={/interface disable ether2 ;/interface enable ether1 ;/system scheduler disable ISP2 ;/system scheduler enable ISP1}

Add to the task scheduler:

  • system scheduler add comment=”” disabled=no interval=25s name=ISP1 on-event=”ISP1DOWN” policy=reboot,read,write,policy,test,password,sniff,sensitive start-date=jan/01/1970 start-time=00:00:00
  • system scheduler add comment=”” disabled=no interval=25s name=ISP2 on-event=”ISP2DOWN” policy=reboot,read,write,policy,test,password,sniff,sensitive start-date=jan/01/1970 start-time=00:00:00

Naturally, ports 1 and 2 are excluded from the standard bridge; if required, a DHCP client is enabled on them.
And also configured masquerading for both ports:

  • ip firewall nat add chain=srcnat action=masquerade out-interface=ether2

Read Also :