Community discussions

MikroTik App
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Filtering output

Wed Apr 28, 2021 1:30 pm

My script select a specific ip address from the list, and i need this address for change the ospf instence router id. (I had a lot of question in the last two days for this, but i hope this will be the last).
I found the selected address and i can print it too, but i don't need netmask from the end.
Example:
ip address: 10.100.11.22/24
if i have a fix length ip format. (the first octet is fix, the 2nd fix, than the 3rd is only 10-99 and the 4th too) Than i can use :pick. But the length of the address is changing every time i change router >.<
so this isn't a good solution:
:local variable [/ip address find address~"^10\\.100\\.([1-9]{1,3}|[0-9]{2,3})\\.([02-9]{1,3}|[0-9]{2,})"]; 
:put $variable; 
:local ipvar [/ip address get value-name=address number=$variable ];
:put $ipvar;
:local pickvar [:pick $ipvar 0 12];
Because if the address is 10.100.11.222/24 than the script pick only until the 22.
So i want to cut it where the slash is.
Have mikrotik something like grep?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Filtering output  [SOLVED]

Wed Apr 28, 2021 3:19 pm

My script select a specific ip address from the list, and i need this address for change the ospf instence router id. (I had a lot of question in the last two days for this, but i hope this will be the last).
I found the selected address and i can print it too, but i don't need netmask from the end.
Example:
ip address: 10.100.11.22/24
if i have a fix length ip format. (the first octet is fix, the 2nd fix, than the 3rd is only 10-99 and the 4th too) Than i can use :pick. But the length of the address is changing every time i change router >.<
so this isn't a good solution:
:local variable [/ip address find address~"^10\\.100\\.([1-9]{1,3}|[0-9]{2,3})\\.([02-9]{1,3}|[0-9]{2,})"]; 
:put $variable; 
:local ipvar [/ip address get value-name=address number=$variable ];
:put $ipvar;
:local pickvar [:pick $ipvar 0 12];
Because if the address is 10.100.11.222/24 than the script pick only until the 22.
So i want to cut it where the slash is.
Have mikrotik something like grep?



3 examples of same methods, for string, ip-prefix and ip type of data, also with separate subnet with and without "/"
:global test "10.100.66.6/23"
:put [:typeof [$test]]
:put [:pick [:tostr $test] -1 [:find [:tostr $test] "/"]]
:put [:pick [:tostr $test] [:find [:tostr $test] "/"] [:len [:tostr $test]]]
:put [:pick [:tostr $test] ([:find [:tostr $test] "/"]+1) [:len [:tostr $test]]]


:global test 10.100.66.6/23
:put [:typeof [$test]]
:put [:pick [:tostr $test] -1 [:find [:tostr $test] "/"]]
:put [:pick [:tostr $test] [:find [:tostr $test] "/"] [:len [:tostr $test]]]
:put [:pick [:tostr $test] ([:find [:tostr $test] "/"]+1) [:len [:tostr $test]]]

:global test 10.100.66.6
:put [:typeof [$test]]
:put [:pick [:tostr $test] -1 [:find [:tostr $test] "/"]]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Filtering output

Wed Apr 28, 2021 3:41 pm

BONUS: for give dotted notification of subnet:

:global testSubnet 22

:global rextended { 32=255.255.255.255;
                 31=255.255.255.254;
                 30=255.255.255.252;
                 29=255.255.255.248;
                 28=255.255.255.240;
                 27=255.255.255.224;
                 26=255.255.255.192;
                 25=255.255.255.128;
                 24=255.255.255.0;
                 23=255.255.254.0;
                 22=255.255.252.0;
                 21=255.255.248.0;
                 20=255.255.240.0;
                 19=255.255.224.0;
                 18=255.255.192.0;
                 17=255.255.128.0;
                 16=255.255.0.0;
                 15=255.254.0.0;
                 14=255.252.0.0;
                 13=255.248.0.0;
                 12=255.240.0.0;
                 11=255.224.0.0;
                 10=255.192.0.0;
                 9=255.128.0.0;
                 8=255.0.0.0;
                 7=254.0.0.0;
                 6=252.0.0.0;
                 5=248.0.0.0;
                 4=240.0.0.0;
                 3=224.0.0.0;
                 2=192.0.0.0;
                 1=128.0.0.0;
                 0=0.0.0.0
}

:put ($rextended->[:tostr $testSubnet])

Who is online

Users browsing this forum: No registered users and 22 guests