What is type ip-prefix

Hello,

i would like to use the “in” function which works fine for the “lightly” documented type ip-prefix. But i have a
strings that looks like “1.2.3.4/3” how could i convert this string to a ip-prefix type so that
this will work?

/ip route> :global a "1.2.3.4/4"   
/ip route> :global b "1.2.3.4/4"  
/ip route> :put ($a in $b)
false



/ip route> :global a [/ip route get 1 dst-address]     
/ip route> :global b [/ip route get 1 dst-address]     
/ip route> :put ($a in $b)                              
true

thx in advance

meno

I’m not certain, but I think the first example you gave is not valid, so it will always be false.

Initially, I thought using :toip would work, but apparently not with the mask. The string is converted to a blank variable with a type of “nil”
:global a “192.168.0.10/32”
:global b “192.168.0.0/24”
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: str, b: str
:set a [:toip $a]
:set b [:toip $b]
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: nil, b: nil
Funny that if you do it this way, the variable type is “nothing”:
:global a [:toip “192.168.0.10/32”]
:global b [:toip “192.168.0.0/24”]
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: nothing, b: nothing
The :toip conversion works fine without the mask, but the mask is necessary for the “in” function to work:
:global a “192.168.0.10”
:global b “192.168.0.0”
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: str, b: str
:set a [:toip $a]
:set b [:toip $b]
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: ip, b: ip
:put ($a in $b)
false
Remove the quotes and it works normally:
:global a 192.168.0.10/32
:global b 192.168.0.0/24
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: ip-prefix, b: ip-prefix
:put ($a in $b)
true

:toip converts string “192.168.0.10/32” to type ip-prefix and string “192.168.0.10” to type ip.

BTW you can see :globals by: /environment print

What version of RouterOS are you using? I was using 5.22 for the tests above…

Interesting - i used 5.24, and yes 5.22 behaviour is different.

I use 6.0rc11… but still there is no solution.

thx

So i use 6.0rc11 and i get

[admin@mam-wl] > :put [:typeof [:toip "172.16.143.10/13"]]
nil

So why is my toip working totally different. What could be a solution of my problem.

Meno

I came across this thread whilst researching a problem I had involving ip-prefix.

The following snippet works for me on v6.6.

:global a "1.2.3.4/4"
:global myproc [:parse (":global aiprefix " . $a )] 
$myproc 
:put $aiprefix
:put [:typeof  $aiprefix]

It’s stiil broken on 6.15.

You can use this workaround to convert:

:local a "192.168.1.0/24"
:local b ([[:parse ":return $a"]])

:put [:typeof $b]
ip-prefix