Community discussions

MikroTik App
 
akliouev
just joined
Topic Author
Posts: 16
Joined: Wed Dec 25, 2013 9:24 am

"ip route find where' strange behavior

Sun Nov 10, 2024 11:26 am

Greetings,

I have encountered a strange problem that is reproducible on 6.49.13 and 7.16.1:

I have a route in my routing table as so:
[akliouev@tat] > /ip route print detail where comment="foo.bar.com"
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme, B - blackhole, U - unreachable, P - prohibit 
 0 A S  ;;; foo.bar.com
        dst-address=1.2.3.4/32 gateway=10.20.30.40 gateway-status=10.20.30.40 recursive via 172.20.7.254 ether5 distance=1 scope=30 target-scope=10 
[akliouev@tat] >
I try to manipulate that route from a script. On the command line it works like a charm:
[akliouev@tat] > :local foo [ip route find where comment="foo.bar.com"]; put $foo;
*8a
So I have only one match in $foo and can manipulate the route:
[akliouev@tat] > :local foo [ip route find where comment="foo.bar.baz"]; put $foo; /ip route set $foo comment="new comment here"
And the route's comment gets changed

The same approach fails when attempted from a script. Here's an example:
:local anchors {
    {"1.2.3.4/32";"1";"main";"foo.bar.baz"}
    {"0.0.0.0/0";"1";"rtab-direct";"direct DGW"}
}

:foreach a in=$anchors do={
    :local route ($a->0)
    :local distance ($a->1)
    :local table ($a->2)
    :local comment ($a->3)

    :log info [:typeof $route]
    :log info $route
    :log info [:typeof $distance]
    :log info $distance
    :log info [:typeof $table]
    :log info $table
    :log info [:typeof $comment]
    :log info $comment

    :local routes [/ip route find where comment="$comment"]
    :log info "Routes: $routes"
}
When this is run I get the following in the log:
 11:30:50 script,info str
 11:30:50 script,info 1.2.3.4/32
 11:30:50 script,info str
 11:30:50 script,info 1
 11:30:50 script,info str
 11:30:50 script,info main
 11:30:50 script,info str
 11:30:50 script,info foo.bar.baz
 11:30:50 script,info Routes: *201C406C;Routes: *201C4024;Routes: *201C4078;Routes: *800000BA;Routes: *201C4084;Routes: *2
01C400C;Routes: *201C4018;Routes: *800000BB
 11:30:50 script,info str
 11:30:50 script,info 0.0.0.0/0
 11:30:50 script,info str
 11:30:50 script,info 1
 11:30:50 script,info str
 11:30:50 script,info rtab-direct
 11:30:50 script,info str
 11:30:50 script,info direct DGW
 11:30:50 script,info Routes: *201C406C;Routes: *201C4024;Routes: *201C4078;Routes: *800000BA;Routes: *201C4084;Routes: *2
01C400C;Routes: *201C4018;Routes: *800000BB
It seems that my "[/ip route find where comment=$comment]" when executed from the script returns ALL the routes instead of a specific one and the "where comment=$comment" part is completely ignored

Can anyone point out what am I doing wrong?
 
jaclaz
Forum Guru
Forum Guru
Posts: 1923
Joined: Tue Oct 03, 2023 4:21 pm

Re: "ip route find where' strange behavior

Sun Nov 10, 2024 12:13 pm

Maybe "routes" is a reserved variable name, try using "myroute" instead of "routes".

And/or try with put instead of assigning the result to a variable.

Also, you can probably remove the "where" (but that shouldn't change anything):
viewtopic.php?t=210095
 
akliouev
just joined
Topic Author
Posts: 16
Joined: Wed Dec 25, 2013 9:24 am

Re: "ip route find where' strange behavior

Sun Nov 10, 2024 12:21 pm

Thanks for the suggestion, but changing the variable name from "routes" to "myroutes" didn't help the cause :-(
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12534
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 10:10 am

The error is the a-b-c of any programming language: Do not use reserved words as variables name:

comment=$comment


As usual the title of the topic is wrong, is not a problem on RouterOS, is a problem on user knowledge.
NO COMMENT...
 
User avatar
infabo
Forum Guru
Forum Guru
Posts: 1459
Joined: Thu Nov 12, 2020 12:07 pm

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 10:16 am

 
akliouev
just joined
Topic Author
Posts: 16
Joined: Wed Dec 25, 2013 9:24 am

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 10:17 am

The error is the a-b-c of any programming language: Do not use reserved words as variables name:

comment=$comment


As usual the title of the topic is wrong, is not a problem on RouterOS, is a problem on user knowledge.
NO COMMENT...
Right, pardon my noob question, but is there a list of reserved words for routeros?
 
jaclaz
Forum Guru
Forum Guru
Posts: 1923
Joined: Tue Oct 03, 2023 4:21 pm

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 10:41 am

I see, right idea, but wrong cat. :wink:
 
User avatar
Larsa
Forum Guru
Forum Guru
Posts: 1611
Joined: Sat Aug 29, 2015 7:40 pm
Location: The North Pole, Santa's Workshop

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 10:43 am

@akliouev - look for ”Reserved variable names” in the link the link that @Infabo just posted.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12534
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 2:21 pm

No point in looking for pages with Reserved variable names (or Keywords), when the reserved word (not keyword) is written immediately before the equal.


comment=$comment


Before correcting me, think carefully about what you write.
And post the correct link, just in case...
https://help.mikrotik.com/docs/spaces/R ... iablenames
Reserved variable names

All built-in RouterOS properties are reserved variables. Variables that will be defined the same as the RouterOS built-in properties can cause errors. To avoid such errors, use custom designations.

For example, the following script will not work:
{ 
:local type "ether1"; 
/interface print where name=$type; 
}

But will work with different defined variables:
 { 
:local customname "ether1"; 
/interface print where name=$customname; 
}

Reason not written in the document (not so clear example):
type is an interface property (not a Keyword), so it can't be a variable name, the not so clear example was better done as:
wrong: where name=$name
right: where name=$ifName


@infabo: I make mistakes like everyone else, but writing just for the pleasure of contradicting...



For obtain all reserved names on the specific context (not all context have same reserved names) for example, on /ip route:
[test@v7] > /ip route get 0<SPACE><TAB>
about             dhcp             immediate-gw      routing-table         
active            disabled         inactive          scope                 
bgp               distance         is-is             static                
bgp-mpls-vpn      dst-address      local-address     suppress-hw-offload   
blackhole         dynamic          modem             target-scope          
check-gateway     ecmp             ospf              vpn                   
comment           gateway          pref-src          vrf-interface         
connect           hw-offloaded     rip               value-name
just write /ip route get 0<SPACE> and press <TAB> (or two times <TAB> instead of space and tab, or any other way)
"get" display also readonly property (no matter if is readonly, is still a "reserved word"), using "set" instead hide some "reserved words"...
 
User avatar
infabo
Forum Guru
Forum Guru
Posts: 1459
Joined: Thu Nov 12, 2020 12:07 pm

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 3:07 pm

@rextended

I did not want to correct you, I did not even know about this "reserved variable names" thing. I just looked up the docs for reserved keywords. And it did not list these. But aren't you the inventor of the raised index finger? See your first post: "the a-b-c of... is a problem on user knowledge.".

To topic:
So it was not a keyword of the scripting language, it was some ROS scripting magic. So one must know every ROS property. And fear of properties added in the future - creating a conflict with currently working script variables.

So it is better to use prefixed variable names instead?
:local foo_comment = "bar";
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12534
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: "ip route find where' strange behavior

Mon Nov 11, 2024 3:27 pm

But aren't you the inventor of the raised index finger? See your first post: "the a-b-c of... is a problem on user knowledge.".
Really is against post title... never mind...



So it is better to use prefixed variable names instead?
:local foo_comment = "bar";
Good idea but bad applied... "_" is not well accepted... (ignoring "=" error and useless ";" ). Require everytimes and everywhere quotes...
:local "foo_comment" "bar" ; :put "foo_comment is $"foo_comment""

Better something like
:local fooComment "bar"

Who is online

Users browsing this forum: No registered users and 4 guests