How to organize automatic switching of the Internet to a backup channel using Mikrotik as an example?

The article content

How to organize automatic switching of the Internet to a backup channel?

Given

  • 2 wires with Internet from an Internet provider
  • Mikrotik router

Problem

Automatic switching between providers in case of communication problems

Solution

Operating principle.

The main data transmission channel is monitored once a minute.
If the main channel fails (the specified node is unavailable at the IP address), the backup channel is turned on. 
When the main channel becomes available, the opposite occurs switching.

Preparation.

On Mikrotik, two Ethernet interfaces must be connected to providers (any networks with Internet access).
It does not matter which ports they are connected to.

You need to find out from your provider the connection settings - the gateway IP address, network mask and dedicated IP address for the router, and configure the addresses for the corresponding interfaces.
Addresses can be dynamic.

Get data from the mobileproxy partner account and replace the following variables with your values:
{server_id}
{API_TOKEN}

For example, the addresses 192.168.77.1 (the provider within the Mikrotik settings is called "inet1") and 192.168.88.1 (the provider name "inet2").
inet1 is the main channel, inet2 is the backup channel, which will be turned on only if there is no access to the "highly available" node on the Internet

In the code below, replace the addresses with yours.
Create a script via web or Winbox, /system script.
Script name - mobileproxy-send-current-ip  
Script code:

---
# mobileproxy.space
# mikrotik send server current ip

:local ServerID {server_id}
:local MobileproxyAPItoken {API_TOKEN}
:local ServerLocalIP {192.168.77.2}
:global CurrentIP
:local ServerComment ""

/tool ​​fetch url="https://mobileproxy.space/api.html?command=edit_servers&server_id=$ServerID&server_local_ip=$ServerLocalIP&server_static_ip=$CurrentIP&server_comment=$ServerComment" http-header-field="Authorization: Bearer $MobileproxyAPItoken"  
---

Next, create a netwatch, /tool ​​netwatch
down-script="/ip route set [find comment=\"inet1\"] disabled=yes\r\
    \n/ip route set [find comment=\"inet2\"] disabled=no" host=8.8.8.8\r\
  \n/system script run mobileproxy-send-current-ip \
up-script="/ip route set [find comment=\"inet1\"] disabled=no\r\
    \n/ip route set [find comment=\"inet2\"] disabled=yes"\r\
  \n/system script run mobileproxy-send-current-ip

Next, we create routes to two gateways in the routing table with the mandatory comment field and the following content:

/ip route
comment=inet1 distance=1 gateway=192.168.77.1
comment=inet2 distance=2 gateway=192.168.88.1

Stay with us, it will be even more interesting!


Share this article: