Script that checks for availability and adds a route via dhcp.
This is the meaning of the script. Whether it is necessary to check up there is 10.0.0.0.0/8.
And if before then update the route, if not then add it.
script for dhcp-client #does not work
If there is a route, I change the gateway address to the one I received from dchp.
If there is no route, then I add this route with the gateway received from dchp.
The route may not exist or the gateway may change.
Update existing route, or add if not present (or, if exist more than one, remove all and add only one)
:if ($bound = 1) do={
/ip route
:local route [find where dst-address=10.0.0.0/8]
:local rnum [:len $route]
:if ($rnum = 0) do={
# the route NOT exist
add dst-address=10.0.0.0/8 gateway=$"gateway-address"
} else={
:if ($rnum = 1) do={
# at least one route exist
set $route gateway=$"gateway-address" disabled=no
} else={
# there are more than one, remove all existing ones and add a new one
# remove $route
# add dst-address=10.0.0.0/8 gateway=$"gateway-address"
}
}
}
more easy to manage and understand, everytime remove all and add new one
If the route is active route10 will be true.
So there is a route and it is enough to fix the gateway.
If there is no route or it is not active then route10 will not be true and “else” should be executed.
is there an error in my logic?
Where more than one route with dst=10.0.0.0/8 already exist the script do error and stop on 2nd line
Do not count how many dst=10.0.0.0/8 routes already exist
Add a new route if already existant route is not active (if is not active, the route is still present),
and on this way you have two route with dst=10.0.0.0/8, and now happen 1) and 2)
[…] If for some reason the route not exist, the script stop on “:set route10 …”.
your script at no point checks whether the route exists or not, or are existent more than one route,
it simply checks whether it is true or false that the route is active.