-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchk_isp2.script
114 lines (93 loc) · 5.14 KB
/
chk_isp2.script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#####
#
# Switcher between ISPs v.2022-02-06_isp2
# by Cless
#
# Requirements:
# - add comments for every ISP wan interface:
# /interface ethernet
# set comment="-wan1-" name=ether2-wan1
# set comment="-wan2-" name=ether3-wan2
# - add default-gw for every ISP:
# /ip route
# add comment="-gw1-" distance=1 dst-address=0.0.0.0/0 gateway=$ISP1_GW
# add comment="-gw2-" distance=1 dst-address=0.0.0.0/0 gateway=$ISP2_GW
# - add custom routing-tables for every pingServer via every ISP:
# /ip route
# add comment="-chk_isp1_pingServer1-" distance=1 dst-address=77.88.8.1 gateway=$ISP1_GW routing-mark=chk_isp1_pingServer1
# add comment="-chk_isp1_pingServer2-" distance=1 dst-address=208.67.222.222 gateway=$ISP1_GW routing-mark=chk_isp1_pingServer2
# add comment="-chk_isp2_pingServer1-" distance=1 dst-address=77.88.8.1 gateway=$ISP2_GW routing-mark=chk_isp2_pingServer1
# add comment="-chk_isp2_pingServer2-" distance=1 dst-address=208.67.222.222 gateway=$ISP2_GW routing-mark=chk_isp2_pingServer2
#
#####
# VARS
# ifWans comments
:local ifWan1Comment "-wan1-";
:local ifWan2Comment "-wan2-";
# gw from route table comments
:local gw1Comment "-gw1-";
:local gw2Comment "-gw2-";
# pingCount
:local pingCount "5";
# acceptable ping loss percent
:local pingLossPercentAcceptable "50";
# routing tables comments for every pingServer via every ISP
:local "chk_isp1_pingServer1_comment" "-chk_isp1_pingServer1-";
:local "chk_isp1_pingServer2_comment" "-chk_isp1_pingServer2-";
:local "chk_isp2_pingServer1_comment" "-chk_isp2_pingServer1-";
:local "chk_isp2_pingServer2_comment" "-chk_isp2_pingServer2-";
# ifWan names (auto get by comments)
:local ifWan1 [/interface get value-name=name [find where comment~"$ifWan1Comment"]] ;
:local ifWan2 [/interface get value-name=name [find where comment~"$ifWan2Comment"]] ;
# get pingServers (IPs), commonly public reliable DNS servers,
# for example "77.88.8.1" and "208.67.222.222"
# (auto get by comment from route table)
:local pingServer1 [:pick [/ip route get value-name=dst-address [find comment~$"chk_isp1_pingServer1_comment"]] 0 \
[:find [/ip route get value-name=dst-address [find where comment~$"chk_isp1_pingServer1_comment"]] "/"]];
:local pingServer2 [:pick [/ip route get value-name=dst-address [find comment~$"chk_isp1_pingServer2_comment"]] 0 \
[:find [/ip route get value-name=dst-address [find where comment~$"chk_isp1_pingServer2_comment"]] "/"]];
# START CHECK
# check ping to pingServers via appropriate routing tables
:local isp1PingServer1PingReceived [/ping $pingServer1 interface="$ifWan1" count="$pingCount" \
routing-table=[:put [ip route get value-name=routing-mark [find where comment~$"chk_isp1_pingServer1_comment"]]]];
:local isp1PingServer2PingReceived [/ping $pingServer2 interface="$ifWan1" count="$pingCount" \
routing-table=[:put [ip route get value-name=routing-mark [find where comment~$"chk_isp1_pingServer2_comment"]]]];
:local isp2PingServer1PingReceived [/ping $pingServer1 interface="$ifWan2" count="$pingCount" \
routing-table=[:put [ip route get value-name=routing-mark [find where comment~$"chk_isp2_pingServer1_comment"]]]];
:local isp2PingServer2PingReceived [/ping $pingServer2 interface="$ifWan2" count="$pingCount" \
routing-table=[:put [ip route get value-name=routing-mark [find where comment~$"chk_isp2_pingServer2_comment"]]]];
# get packetLoss in percents
:local isp1PingServer1PingLossPercent [(100 - ($isp1PingServer1PingReceived * 100) / $pingCount)];
:local isp1PingServer2PingLossPercent [(100 - ($isp1PingServer2PingReceived * 100) / $pingCount)];
:local isp2PingServer1PingLossPercent [(100 - ($isp2PingServer1PingReceived * 100) / $pingCount)];
:local isp2PingServer2PingLossPercent [(100 - ($isp2PingServer2PingReceived * 100) / $pingCount)];
# debug
:put "isp1PingServer1PingLoss: $isp1PingServer1PingLossPercent %";
:put "isp1PingServer2PingLoss: $isp1PingServer2PingLossPercent %";
:put "isp2PingServer1PingLoss: $isp2PingServer1PingLossPercent %";
:put "isp2PingServer2PingLoss: $isp2PingServer2PingLossPercent %";
# get enabled and disabled gws
:local gw1disabled [/ip route get value-name=disabled [find comment~"$gw1Comment"]] ;
:local gw2disabled [/ip route get value-name=disabled [find comment~"$gw2Comment"]] ;
# depending on ping loss percent set active gw
:if ($isp1PingServer1PingLossPercent <= $pingLossPercentAcceptable || $isp1PingServer2PingLossPercent <= $pingLossPercentAcceptable) do={
# gw1 ON
:if ($gw1disabled = true) do={
:put "gw1 ON!";
/ip route enable [find comment~"$gw1Comment"];
} else={ :put "gw1 enabled, nothing to do"; };
:if ($gw2disabled = false) do={
:put "gw2 OFF!";
/ip route disable [find comment~"$gw2Comment"];
} else={ :put "gw2 disabled, nothing to do"; };
} else={
# gw2 ON
:if ($gw1disabled = false) do={
:put "gw1 OFF!";
/ip route disable [find comment~"$gw1Comment"];
} else={ :put "gw1 disabled, nothing to do"; };
:if ($gw2disabled = true) do={
:put "gw2 ON!";
/ip route enable [find comment~"$gw2Comment"];
} else={ :put "gw2 enabled, nothing to do"; };
}