Код: Выделить всё
# Функция определения активного шлюза (маршрута в Мир)
# not for ROS7 !
# by Sertik version 03/11/2020
# update 22/09/2022
# если активных маршрутов нет возвращает "not found"
# если нет активных маршрутов, через которые пингуется $pingAdr возвращает "not reachable"
# при наличии активного маршрута, достигающего пингом $pingAdr возвращает gateway, а
# в global ActiveRoutingMark дополнительно возвращает метку (routing-mark) активного маршрута (если он не меченный, метка пуста)
:global FuncActIface do={
:if ([:len $0]!=0) do={
:local GatewayStatus; :local activeGateway; :local Gdistance; :local activeInterface; :local RoutingMark;
:local ActIfacePing; :global ActiveRoutingMark;
:local ACdistance; :local ACGetway; :local ACRMark;
:local counter; :local Keep 0;
:local pingAdr 8.8.8.8;
:local pingCount 5;
:local PingActNum 3;
/ip route
:foreach counter in=[find dst-address=0.0.0.0/0] do={
:if ([get $counter active] = true) do={
:set $activeGateway [get $counter gateway];
:set $Gdistance [get $counter distance];
:set $RoutingMark [get $counter routing-mark];
:if ($activeGateway!=nothing) do={
:if ([:len $RoutingMark]!=0) do={
:set $GatewayStatus [ :tostr [ /ip route get \
[ find gateway=$activeGateway dst-address=0.0.0.0/0 \
distance=$Gdistance routing-mark=$RoutingMark] gateway-status]];
} else={
:set $GatewayStatus [ :tostr [ /ip route get \
[ find gateway=$activeGateway dst-address=0.0.0.0/0 \
distance=$Gdistance] gateway-status]];}
:if ([:find $GatewayStatus "via"] > 0) do={
:set $activeInterface [ :pick $GatewayStatus \
([ :len [ :pick $GatewayStatus 0 [ :find $GatewayStatus "via" ] ] ] + [:len "via"] +2 ) \
[ :len $GatewayStatus ]];
} else={
:set $activeInterface [ :pick $GatewayStatus 0 ([:find $GatewayStatus "reachable"]-1)];
:if ([:len [:pick $GatewayStatus [:find $GatewayStatus "reachable"] [:len $GatewayStatus]]]>[:len "reachable"]) do={
:set $activeInterface [:pick $GatewayStatus ([:find $GatewayStatus "reachable"]+[:len "reachable"]+1) [:len $GatewayStatus]]}}
:if ([:len $RoutingMark]=0) do={
:set $ActIfacePing [/ping $pingAdr interface=$activeInterface count=$pingCount];
} else={
:set $ActIfacePing [/ping $pingAdr interface=$activeInterface count=$pingCount routing=$RoutingMark]}
:if ($ActIfacePing>=$PingActNum) do={
:if ($Keep=0) do={
:set $ACGetway $activeInterface;
:set $ACdistance $Gdistance; :set $ACRMark $RoutingMark;
:set $ActiveRoutingMark $RoutingMark; :set $Keep ($Keep+1)}
:if (($Keep>0) && ($Gdistance<=$ACdistance)) do={
:set $ACGetway $activeInterface; :set $ACdistance $Gdistance;
:set $ACRMark $RoutingMark; :set $ActiveRoutingMark $RoutingMark;
:set $Keep ($Keep+1)}
} else={:set $activeInterface "not reacheable";}
} else={:set $activeInterface "not found";}
}
}
:if ($Keep!=0) do={:return $ACGetway} else={:return $activeInterface}
}
}
# :log info [$FuncActIface]