E
E
EnumaElis2018-03-07 16:39:25
Mikrotik
EnumaElis, 2018-03-07 16:39:25

two providers. How to check a previously dropped channel with a script?

Hello.
For some reason, the failover implementation is needed through a script. I saw several scripts, I took one of them, slightly modifying it:

:local MainIf "ether1";
:local RsrvIf "ether5";
:local PingCount 5;
:local PingTargets {78.88.8.8; 87.250.250.203; 8.8.8.8};
:local MainIfInetOk false;
:local RsrvIfInetOk false;
:local MainPings 0;
:local RsrvPings 0;
:local mikroName [/system identity get name];
:local mikroDate [/system clock get date];
:local mikroTime [/system clock get time];

:log info ("== START PING to $PingTargets ==");

foreach host in=$PingTargets do={
 :local res [/ping $host count=$PingCount interface=$MainIf];
 :set MainPings ($MainPings + $res);
 :local res [/ping $host count=$PingCount interface=$RsrvIf];
 :set RsrvPings ($RsrvPings + $res);
}

:log info ("== Main pings success: $MainPings ==");
:log info ("== Rsrv pings success: $RsrvPings ==");

:set MainIfInetOk ($MainPings >= 10);
:set RsrvIfInetOk ($RsrvPings >= 10);

:local MainGWDistance [/ip route get [find comment="ISP1"] distance];
:local RsrvGWDistance [/ip route get [find comment="ISP2"] distance];

if ($MainIfInetOk && ($MainGWDistance >= $RsrvGWDistance)) do={
 /ip route set [find comment="ISP1"] distance=1;
 /ip route set [find comment="ISP2"] distance=2;
 foreach i in=[/ip firewall connection find dst-address~":1723"] do={/ip firewall connection remove $i};
 /tool e-mail send from="[email protected]" server=10.10.10.10 to="[email protected]" \ 
 body=("$mikroName switched to _PRIMARY_ ISP at ($mikroDate in $mikroTime)") subject="Mikrotik: $mikroName INTERNET MAIN UP";
 /log info ">>> Switched to main internet connection";
}

if (!$MainIfInetOk && $RsrvIfInetOk && ($MainGWDistance <= $RsrvGWDistance)) do={
 /ip route set [find comment="ISP1"] distance=2;
 /ip route set [find comment="ISP2"] distance=1;
 foreach i in=[/ip firewall connection find dst-address~":1723"] do={/ip firewall connection remove $i};
 /tool e-mail send from="[email protected]" server=10.10.10.10 to="[email protected]" \
 body=("$mikroName switched to _SECONDARY_ ISP at ($mikroDate in $mikroTime)") subject="Mikrotik: $mikroName INTERNET MAIN DOWN";
 /log info ">>> Switched to reserved internet connection";
}

:log info ("== DUAL ISP script end ==");

/ip route
add comment=ISP1 distance=1 gateway=IP_GW1
add comment=ISP2 distance=2 gateway=IP_GW2

The script is processed once a minute. In the event of a failure of the main channel, it successfully switches to the backup channel, resets VPN connections and sends a letter.
The problem is that, as I found out by googling, you cannot ping through IPv4: interfaces , from there:
interface (string; Default: )	Which interface to use (required when pinging IPv6 address)

A reasonable question is, how could these scripts work for people at all?
Those. this part of the script: "/ping $host count=$PingCount interface=$MainIf", when switching the distance, always returns "no response from the host", even though the main channel could already come back to life.
How can I check if the main provider is back in service?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
l0ser140, 2018-03-07
@l0ser140

The interface option does not work with ipv4. And with ipv6 it doesn't do what you want.
As a parameter to ping, you must specify a routing-table and have routes through gateways 1 and 2 of the provider, respectively, marked with the corresponding route labels.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question