Community discussions

MikroTik App
 
nimda
just joined
Topic Author
Posts: 9
Joined: Thu Dec 15, 2022 1:11 pm

Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 1:41 pm

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
:global route10;
:set route10 [/ip route get value-name=active [find dst-address=10.0.0.0/8]];
:if ($bound=1) do={:if ($route10=true) do={/ip route set [find dst-address=10.0.0.0/8] gateway=$"gateway-address" } else={/ip route add dst-address=10.0.0.0/8 gateway=$"gateway-address"}};
[/code]

#does not work
[code]
:global route10;
:set route10 [ip route get value-name=active [find dst-address=10.0.0.0/8]];
:if (($bound=1) and ($route10=true)) do={/ip route set [find dst-address=10.0.0.0/8] gateway=$"gateway-address" };
:if (($bound=1) and ($route10!=true)) do={/ip route add dst-address=10.0.0.0/8 gateway=$"gateway-address" };
[/code]

Why my script does not work?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:02 pm

on first script, you check if the route is active or not,
if is not active.... you add again same route... but is not missing, is inactive...

translated with correct syntax, but not fixed:
:if ($bound = 1) do={
    :if ([/ip route get [find where dst-address=10.0.0.0/8] active]) do={
        /ip route set [find where dst-address=10.0.0.0/8] gateway=$"gateway-address"
    } else={
        /ip route add dst-address=10.0.0.0/8 gateway=$"gateway-address"
    }
}

The second script unnecessarily tangles everything, but corrected the syntax and the logic, it does exactly as above.
 
nimda
just joined
Topic Author
Posts: 9
Joined: Thu Dec 15, 2022 1:11 pm

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:10 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:20 pm

Two way for solve

1) 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"
        }
    }
}

2) more easy to manage and understand, everytime remove all and add new one
:if ($bound = 1) do={
    /ip route
    remove [find where dst-address=10.0.0.0/8]
    add dst-address=10.0.0.0/8 gateway=$"gateway-address"
}
 
nimda
just joined
Topic Author
Posts: 9
Joined: Thu Dec 15, 2022 1:11 pm

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:21 pm

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?
 
nimda
just joined
Topic Author
Posts: 9
Joined: Thu Dec 15, 2022 1:11 pm

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:26 pm

A tunnel has been built through this route. If you delete the route, the tunnel will drop every time.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:30 pm

is there an error in my logic?
some:
1) Where more than one route with dst=10.0.0.0/8 already exist the script do error and stop on 2nd line
2) Do not count how many dst=10.0.0.0/8 routes already exist
3) 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)
4) etc.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:30 pm

A tunnel has been built through this route. If you delete the route, the tunnel will drop every time.
You can use my script no.1) on post #4 on this case, it change only the gateway of existant route, if any, or add the ruote if is missing.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:36 pm

If there is no route or it is not active then route10 will not be true and "else" should be executed.
I forgot to write:
1) [...] If for some reason the route not exist, the script stop on ":set route10 ...".
5) 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.
 
nimda
just joined
Topic Author
Posts: 9
Joined: Thu Dec 15, 2022 1:11 pm

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 2:55 pm

If there is no route or it is not active then route10 will not be true and "else" should be executed.
I forgot to write:
1) [...] If for some reason the route not exist, the script stop on ":set route10 ...".
5) 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.
"1) [...] If for some reason the route not exist, the script stop on ":set route10 ..."."
This is a fact I did not know.

How to check if the route exists or not.
How to correctly write the result to a variable in boolean type.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 3:03 pm

How to check if the route exists or not.
How to correctly write the result to a variable in boolean type.
You do not read my post #4?

you can not do it directly, and everytime you have 3 alternatives: not exist, exist only one, exist more than one:
{
    :local route [/ip route find where dst-address=10.0.0.0/8]
    :local rnum  [:len $route]
    :if ($rnum = 0) do={
        :put "the route NOT exist"
    } else={
        :if ($rnum = 1) do={
            :put "only ONE route exist"
        } else={
            :put "there are MORE than one route"
        }
    }
}
 
nimda
just joined
Topic Author
Posts: 9
Joined: Thu Dec 15, 2022 1:11 pm

Re: Script that checks for availability and adds a route via dhcp.

Thu Dec 15, 2022 3:36 pm

Thanks for the help.

Who is online

Users browsing this forum: No registered users and 23 guests