:local firstInterface "pppoe-rostelecom"; :local secondInterface "eth2-MTS"; :local pingTo1 "8.8.8.8"; :local pingTo2 "217.112.35.75"; :local pingCount 5; :local stableConnectFrom 70; :local prefix ">>> "; :local firstInterfaceName $firstInterface; :local secondInterfaceName $secondInterface;
:local clearArp do={ :local dumplist [/ip arp find] :foreach i in=$dumplist do={ /ip arp remove $i } :log info ($prefix . "ARP cleaned"); }
# Function to reconnect PPPoE connection :local reconnectPPPoE do={ /interface pppoe-client set $nameInterface disable=yes; :delay 1s; /interface pppoe-client set $nameInterface disable=no; }
# Check FIRST interface /interface pppoe-client { :if ( [get $firstInterface disable] = true) do={ set $firstInterface disable=no; :delay 5s; } } # Check SECOND interface /interface ethernet { :if ( [get $secondInterface disable] = true) do={ set $secondInterface disable=no; } }
/ip route { # Set objects to variables :set firstInterface [find dst-address="0.0.0.0/0" gateway=$firstInterfaceName]; :set secondInterface [find dst-address="0.0.0.0/0" gateway=$secondInterfaceName]; # Check routes :if ( [get $firstInterface distance] != 2 ) do={ set $firstInterface distance=2; :log info ($prefix . "Distance for " . $firstInterfaceName . " corrected"); } :if ( [get $secondInterface distance] != 1 && [get $secondInterface distance] != 3) do={ set $secondInterface distance=3; :log info ($prefix . "Distance for " . $secondInterfaceName . " corrected"); } # ... body ... }
/ip route { # ... :local pingStatus \ ((( [/ping $pingTo1 interface=$firstInterfaceName count=$pingCount] + \ [/ping $pingTo2 interface=$firstInterfaceName count=$pingCount] ) / ($pingCount * 2)) * 100); :if ( [get $firstInterface active] = false or $pingStatus < $stableConnectFrom) do={ # ... } # ... }
:if ( [get $firstInterface active] = false or $pingStatus < $stableConnectFrom) do={ :log info ($prefix . "FIRST NO INTERNET!!!"); # Change distance :if ( [get $secondInterface distance] != 1 ) do={ set $secondInterface distance=1; :log info ($prefix . "Distance for " . $secondInterfaceName . " changed"); $clearArp; } $reconnectPPPoE nameInterface=$firstInterfaceName; }
/ip route { # ... :if ( [get $firstInterface active] = false or $pingStatus < $stableConnectFrom) do={ # ... } else={ :log info ($prefix . "FIRST INTERNET CONNECTED"); # Change distance :if ( [get $secondInterface distance] != 3 ) do={ set $secondInterface distance=3; :log info ($prefix . "Distance for " . $secondInterfaceName . " changed"); $clearArp; } } # ... }
# Set local variables :local firstInterface "pppoe-rostelecom"; :local secondInterface "eth2-MTS"; :local pingTo1 "8.8.8.8"; :local pingTo2 "217.112.35.75"; :local pingCount 5; :local stableConnectFrom 70; :local prefix ">>> "; # Local variables :local firstInterfaceName $firstInterface; :local secondInterfaceName $secondInterface; # Function to cleaning ARP table :local clearArp do={ :local dumplist [/ip arp find] :foreach i in=$dumplist do={ /ip arp remove $i } :log info ($prefix . "ARP cleaned"); } # Function to reconnect PPPoE connection :local reconnectPPPoE do={ /interface pppoe-client set $nameInterface disable=yes; :delay 1s; /interface pppoe-client set $nameInterface disable=no; } :log info ($prefix . "START PING to $pingTo1 & $pingTo2"); # Check FIRST interface /interface pppoe-client { :if ( [get $firstInterface disable] = true) do={ set $firstInterface disable=no; :delay 5s; } } # Check SECOND interface /interface ethernet { :if ( [get $secondInterface disable] = true) do={ set $secondInterface disable=no; } } /ip route { # Set objects to variables :set firstInterface [find dst-address="0.0.0.0/0" gateway=$firstInterfaceName]; :set secondInterface [find dst-address="0.0.0.0/0" gateway=$secondInterfaceName]; # Check routes :if ( [get $firstInterface distance] != 2 ) do={ set $firstInterface distance=2; :log info ($prefix . "Distance for " . $firstInterfaceName . " corrected"); } :if ( [get $secondInterface distance] != 1 && [get $secondInterface distance] != 3) do={ set $secondInterface distance=3; :log info ($prefix . "Distance for " . $secondInterfaceName . " corrected"); } # Get ping successfully packets. In percent :local pingStatus \ ((( [/ping $pingTo1 interface=$firstInterfaceName count=$pingCount] + \ [/ping $pingTo2 interface=$firstInterfaceName count=$pingCount] ) / ($pingCount * 2)) * 100); # Check Internet :if ( [get $firstInterface active] = false or $pingStatus < $stableConnectFrom) do={ :log info ($prefix . "FIRST NO INTERNET!!!"); # Change distance :if ( [get $secondInterface distance] != 1 ) do={ set $secondInterface distance=1; :log info ($prefix . "Distance for " . $secondInterfaceName . " changed"); $clearArp; } $reconnectPPPoE nameInterface=$firstInterfaceName; } else={ :log info ($prefix . "FIRST INTERNET CONNECTED"); # Change distance :if ( [get $secondInterface distance] != 3 ) do={ set $secondInterface distance=3; :log info ($prefix . "Distance for " . $secondInterfaceName . " changed"); $clearArp; } } } :log info ($prefix . "END PING");
:local pingStatus \ (( [/ping $pingTo1 interface=$firstInterfaceName count=$pingCount] + \ [/ping $pingTo2 interface=$firstInterfaceName count=$pingCount] ) / ($pingCount * 2)) * 100;
:local pingStatus \ ((( [/ping $pingTo1 interface=$firstInterfaceName count=$pingCount] + \ [/ping $pingTo2 interface=$firstInterfaceName count=$pingCount] ) / ($pingCount * 2)) * 100);
Source: https://habr.com/ru/post/279567/
All Articles