/ip dhcp-client set [/ip dhcp-client find interface=$Iface] add-default-route=yes [no]
:delay 10s :local Iface "isp1" :local StatusIface :local CurrentGateway :local pingInet :local pingLink :local pingGateway :local IPToPingInet "213.180.193.3" :local IPToPing "8.8.4.4" :local PingCount 5 :local Margin 1 :local Distance 1 :local DistanceDefault 10 :local RunTime 0 :local TimeToWait 20 # while (true) do={ # :set pingInet [/ping $IPToPingInet count=$PingCount interface=$Iface] :log debug "$pingInet $Iface $IPToPingInet" :if ($pingInet < ($PingCount-$Margin)) do={ :log error "No internet connection on $Iface." /ip dhcp-client set [/ip dhcp-client find interface=$Iface] add-default-route=no # :while ($pingInet < ($PingCount-$Margin)) do={ # :set pingLink [/ping $IPToPing count=$PingCount interface=$Iface] :log debug "$pingLink $Iface $IPToPing" :if ($pingLink < ($PingCount-$Margin)) do={ # /interface ethernet disable $Iface; /interface ethernet enable $Iface :while ($pingLink < ($PingCount-$Margin)) do={ :log debug "$pingLink $Iface $IPToPing" :set RunTime ($RunTime + 1) :log debug $RunTime # Time to wait :if ( $RunTime = $TimeToWait ) do={ # Reboot interface :log info "reboot and release $Iface" /interface ethernet disable $Iface; /interface ethernet enable $Iface :set RunTime 0 } # :if ([/interface ethernet get $Iface disabled] = false) do={ :log debug "Interface $Iface enabled" # /interface ethernet monitor $Iface once do={ :set StatusIface $status } :if ($StatusIface = "link-ok") do={ :log debug "$Iface link-ok." # dhcp :if ([/ip dhcp-client find interface=$Iface] != "") do={ :log debug "test1" # DHCP :if ( [/ip dhcp-client get [/ip dhcp-client find interface=$Iface] invalid ] != true) do={ :log debug "test2" # DHCP lease :set CurrentGateway [/ip dhcp-client get [/ip dhcp-client find interface=$Iface] gateway ] :log debug "Waiting DHCP lease" :if ($CurrentGateway != nil) do={ :set CurrentGateway [:put ("$CurrentGateway" . "%$Iface")] :log debug "CurrentGateway $CurrentGateway" # Looking for route test :log debug "Cheking test route for $Iface..." :local a [ /ip route find comment="$Iface" ] :if (($a) = "") do={ :log info ("Adding test route for $Iface...") /ip route add dst-address=$IPToPing gateway=$CurrentGateway comment="$Iface" distance=$Distance } else={ :local EstablishedGateway [/ip route get [/ip route find comment="$Iface"] gateway ] :log debug "EstablishedGateway $EstablishedGateway" :if ( $CurrentGateway = $EstablishedGateway ) do={ :log debug "No route changes needed for $Iface." } else={ :log info "Updating test route for $Iface..." /ip route set [/ip route find comment="$Iface" ] dst-address=$IPToPing gateway=$CurrentGateway comment="$Iface" distance=$Distance } } :set pingGateway [/ping [/ip dhcp-client get [/ip dhcp-client find interface=$Iface] gateway ] count=$PingCount interface=$Iface] :log debug "$pingGateway $Iface $IPToPing" :if ($pingGateway < ($PingCount-$Margin)) do={ :log error "route error on $Iface" :log debug [/ip dhcp-client get [/ip dhcp-client find interface=$Iface] gateway ] /ip dhcp-client release [/ip dhcp-client find interface=$Iface] } } else={ :log error "DHCP no lease." :delay 1s } } else={ :log error "DHCP failure on $Iface." :log info "reboot and release $Iface" /interface ethernet disable $Iface; /interface ethernet enable $Iface :delay 1s } } else={ :log info "adding DHCP client for $Iface" /ip dhcp-client add interface=$Iface disabled=no add-default-route=yes default-route-distance=$DistanceDefault use-peer-dns=no use-peer-ntp=no } } else={ :log debug "No-link on $Iface." :delay 1s } } else={ :log error "Interface $Iface disabled." } :set pingLink [/ping $IPToPing count=$PingCount interface=$Iface] } } else={ :log info "add default route= yes for $Iface" /ip dhcp-client set [/ip dhcp-client find interface=$Iface] add-default-route=yes } :set pingInet [/ping $IPToPingInet count=$PingCount interface=$Iface] } } else={ :log debug "Internet on $Iface connected." } }
/system scheduler add name=CheckTestRoute1 start-time=startup on-event=CheckTestRoute1
Source: https://habr.com/ru/post/243071/
All Articles