Mikrotik: automatic channel switching to the backup and back
I was prompted to write this post by disconnecting one of the Internet channels.
On the Internet itself there are many answers to this question, but not everyone is working.
What I wanted to do if the main channel of the Internet is disconnected:
1. Switch to the backup channel (after “appearing”, of course, return to the main channel);
2. Send a notification by email about the fact of a state change.
Who cares, please, under the cat.
We were given:
- Mikrotik RB450G with firmware 5.19 version;
- 2 Internet ports, one of which uses a PPPoE connection to connect.
First, add 2 scripts, one of which will switch to the backup channel, and the second will return the connection to the first.

We compose the first script that will activate the backup channel and call it " change-to-reserv " and contain the code:
(Note: the IP address 1.1.1.1 is selected as an example and symbolizes the backup channel)
That is, if there is no ping to the server (more on that later), we will turn off the route with a gateway pointing to " pppoe-main ".
PS: After erazel’s comment , this scheme was improved, namely, the earlier script switched between two routes that failed, namely, if you run ping google.ru -t , for example, from the computer, the ping will go to the old one interface, because the broadcast has not been updated. In the proposed method of changing only the gateway, translation clearing is not required.
We indicate the following line in the same script:
where:
/ tool e-mail send - send a notification to the administrator’s email about the fact that the status change
server = 192.168.1.1 - SMTP server. Since we use our own, I indicate it;
port = 25 - in RouterOS 5.x version, the port is specified separately. In our case, it defaults to 25;
user=robot@mysite.ru - user login for authorization on the SMTP server (if required);
password = 1PaSsW0rD1 - specify the password (if required);
tls = no - TLS traffic encryption. We don’t have it, I put “no”, and if so, “yes”;
to=admin@mysite.ru - which email address will the notification receive;
from = "ROBOT" - from whom the notification will come (in my case, it matches the authorization login. The address of the sender is indicated in brackets, and in front of it the name displayed in the incoming mail);
subject = "MikroTik: $ [/ system clock get date], $ [/ system clock get time]" - indicate the title of the message. In this case, it will look like “MikroTik: jul / 30/2014, 10:52:13” (date and time of sending the message);
body = "Switching to the backup channel \ nDate: $ [/ system clock get date] \ nATime: $ [/ system clock get time]"; - accordingly, the message body itself, which will look like:
As a result, our script will look like (RouterOS 5.19):
And for RouterOS 6.17:
As already mentioned above, save it under the name " change-to-reserv " and proceed to write the second script:
Unlike the first script, in the body of the sent email message we will indicate “Switch to the main channel” and enable the previously disabled route.
Save our script under the name " change-to-main ".
Since Mikrotik’s memory is not rubber, we optimize our script to complete the task.
To do this, we need to use the Netwatch utility , which works as a trigger. That is, if the connection status changes, then the status changes with the execution of the scripts we need.

In Netwatch, we will add a new rule, where we specify the host 8.8.8.8 and the script names in the Up tabs - “change-to-main” and “change-to-reserv”in the “Down” tab , respectively.
You should also indicate the period for checking the status. We have indicated 1 minute .

Then comes the final step - route forwarding. If this is not done, then the script will work to switch to the backup channel and remain in this position. The reverse transition will be possible if the backup channel "falls".
In general, we add a route with the following data:
Dst. Address = 8.8.8.8 // We indicate that we will ping the Google DNS server (not critical for me, I indicate it);
Gateway = pppoe-main // The same PPPoE connection to the main channel
Distance = 1 We
leave the rest of the parameters as they are.

All!
From now on, the principle of operation is as follows:
Netwatch through the main channel will check ping to the Google DNS server. As soon as the ping disappears, the " change-to-reserv " script specified on the " Down " tab will be executed . This script will disable the main route (PPPoE) and all packets will go on the backup channel. As soon as ping on the main channel resumes, the script activates the route of the main channel again (the Distance parameter of which, of course, is “ 1 ”, and the backup parameter is “ 2 ”). Along with this, notifications will be sent to the email address about the facts of the state change.
Profit!
ATTENTION!!! For scripts running RouterOS 6.17 to work, you need to make changes to the script for sending email addresses, namely, remove the " tls = " parameter .
That is, our code (for example, to switch to the backup channel) will look like:
UPDATED: Changed routes in scripts
UPDATED 2: To prevent the email address “robot@mysite.ru” from appearing in the incoming mail, the “from” parameter was changed (corrections with comments were added to the code above)
On the Internet itself there are many answers to this question, but not everyone is working.
What I wanted to do if the main channel of the Internet is disconnected:
1. Switch to the backup channel (after “appearing”, of course, return to the main channel);
2. Send a notification by email about the fact of a state change.
Who cares, please, under the cat.
We were given:
- Mikrotik RB450G with firmware 5.19 version;
- 2 Internet ports, one of which uses a PPPoE connection to connect.
First, add 2 scripts, one of which will switch to the backup channel, and the second will return the connection to the first.

We compose the first script that will activate the backup channel and call it " change-to-reserv " and contain the code:
/ip route set gateway=1.1.1.1 [find dst-address=0.0.0.0/0];(Note: the IP address 1.1.1.1 is selected as an example and symbolizes the backup channel)
That is, if there is no ping to the server (more on that later), we will turn off the route with a gateway pointing to " pppoe-main ".
PS: After erazel’s comment , this scheme was improved, namely, the earlier script switched between two routes that failed, namely, if you run ping google.ru -t , for example, from the computer, the ping will go to the old one interface, because the broadcast has not been updated. In the proposed method of changing only the gateway, translation clearing is not required.
We indicate the following line in the same script:
/tool e-mail send server=192.168.1.1 port=25 user=robot@mysite.ru password=1PaSsW0rD1 tls=no to=admin@mysite.ru from="ROBOT" \
subject="MikroTik: $[/system clock get date], $[/system clock get time]" \
body="Переключение на резервный канал\nДата: $[/system clock get date]\nAВремя: $[/system clock get time]"; where:
/ tool e-mail send - send a notification to the administrator’s email about the fact that the status change
server = 192.168.1.1 - SMTP server. Since we use our own, I indicate it;
port = 25 - in RouterOS 5.x version, the port is specified separately. In our case, it defaults to 25;
user=robot@mysite.ru - user login for authorization on the SMTP server (if required);
password = 1PaSsW0rD1 - specify the password (if required);
tls = no - TLS traffic encryption. We don’t have it, I put “no”, and if so, “yes”;
to=admin@mysite.ru - which email address will the notification receive;
from = "ROBOT
subject = "MikroTik: $ [/ system clock get date], $ [/ system clock get time]" - indicate the title of the message. In this case, it will look like “MikroTik: jul / 30/2014, 10:52:13” (date and time of sending the message);
body = "Switching to the backup channel \ nDate: $ [/ system clock get date] \ nATime: $ [/ system clock get time]"; - accordingly, the message body itself, which will look like:
Switching the line to the backup channel
Date: jul /
30/2014 Time: 10:52:13
As a result, our script will look like (RouterOS 5.19):
/ip route set gateway=1.1.1.1 [find dst-address=0.0.0.0/0];
/tool e-mail send server=192.168.1.1 port=25 user=robot@mysite.ru password=1PaSsW0rD1 tls=no to=admin@mysite.ru from="ROBOT" \
subject="MikroTik: $[/system clock get date], $[/system clock get time]" \
body="Переключение на резервный канал\nДата: $[/system clock get date]\nAВремя: $[/system clock get time]"; And for RouterOS 6.17:
/ip route set gateway=1.1.1.1 [find dst-address=0.0.0.0/0];
/tool e-mail send server=192.168.1.1 port=25 user=robot@mysite.ru password=1PaSsW0rD1 to=admin@mysite.ru from="ROBOT" \
subject="MikroTik: $[/system clock get date], $[/system clock get time]" \
body="Переключение на резервный канал\nДата: $[/system clock get date]\nAВремя: $[/system clock get time]"; As already mentioned above, save it under the name " change-to-reserv " and proceed to write the second script:
/ip route set gateway=pppoe-main [find dst-address=0.0.0.0/0];
/tool e-mail send server=192.168.1.1 port=25 user=robot@mysite.ru password=1PaSsW0rD1 tls=no to=admin@mysite.ru from="ROBOT" \
subject="MikroTik: $[/system clock get date], $[/system clock get time]" \
body="Переключение на основной канал\nДата: $[/system clock get date]\nAВремя: $[/system clock get time]"; Unlike the first script, in the body of the sent email message we will indicate “Switch to the main channel” and enable the previously disabled route.
Save our script under the name " change-to-main ".
Since Mikrotik’s memory is not rubber, we optimize our script to complete the task.
To do this, we need to use the Netwatch utility , which works as a trigger. That is, if the connection status changes, then the status changes with the execution of the scripts we need.

In Netwatch, we will add a new rule, where we specify the host 8.8.8.8 and the script names in the Up tabs - “change-to-main” and “change-to-reserv”in the “Down” tab , respectively.
You should also indicate the period for checking the status. We have indicated 1 minute .

Then comes the final step - route forwarding. If this is not done, then the script will work to switch to the backup channel and remain in this position. The reverse transition will be possible if the backup channel "falls".
In general, we add a route with the following data:
Dst. Address = 8.8.8.8 // We indicate that we will ping the Google DNS server (not critical for me, I indicate it);
Gateway = pppoe-main // The same PPPoE connection to the main channel
Distance = 1 We
leave the rest of the parameters as they are.

All!
From now on, the principle of operation is as follows:
Netwatch through the main channel will check ping to the Google DNS server. As soon as the ping disappears, the " change-to-reserv " script specified on the " Down " tab will be executed . This script will disable the main route (PPPoE) and all packets will go on the backup channel. As soon as ping on the main channel resumes, the script activates the route of the main channel again (the Distance parameter of which, of course, is “ 1 ”, and the backup parameter is “ 2 ”). Along with this, notifications will be sent to the email address about the facts of the state change.
Profit!
ATTENTION!!! For scripts running RouterOS 6.17 to work, you need to make changes to the script for sending email addresses, namely, remove the " tls = " parameter .
That is, our code (for example, to switch to the backup channel) will look like:
/ip route set gateway=1.1.1.1 [find dst-address=0.0.0.0/0];
/tool e-mail send server=192.168.1.1 port=25 user=robot@mysite.ru password=1PaSsW0rD1 to=admin@mysite.ru from="ROBOT" \
subject="MikroTik: $[/system clock get date], $[/system clock get time]" \
body="Переключение на резервный канал\nДата: $[/system clock get date]\nAВремя: $[/system clock get time]"; UPDATED: Changed routes in scripts
UPDATED 2: To prevent the email address “robot@mysite.ru” from appearing in the incoming mail, the “from” parameter was changed (corrections with comments were added to the code above)