Community discussions

MikroTik App
 
lz1dsb
Member Candidate
Member Candidate
Topic Author
Posts: 226
Joined: Wed Aug 07, 2013 11:48 am

Filtering outputs on print commands? How?

Wed Aug 20, 2014 5:45 pm

I don't know if this is the right place to put this...

How can I filter the output of any print command in RouterOS?
for example: ip route print - how can I get only the connected, static, etc... it does have embedded filter for BGP and OSPf though - it works.
But how can I flexibly define what I want to be displayed? I'm running full table BGP now and it's really needed.

I also need to filter output on the ip firewall connection print command.

I know there's the 'where' option, but it does not seem to work, or very likely I don't use it properly.
There's also a 'find' option, what's the difference?

Is there a nice guide explaining these command options?
 
User avatar
payday
Member Candidate
Member Candidate
Posts: 233
Joined: Thu Aug 16, 2012 11:05 pm

Re: Filtering outputs on print commands? How?

Thu Aug 21, 2014 12:32 am

How can I filter the output of any print command in RouterOS?
for example: ip route print - how can I get only the connected, static, etc... it does have embedded filter for BGP and OSPf though - it works.
But how can I flexibly define what I want to be displayed? I'm running full table BGP now and it's really needed.
Is there a nice guide explaining these command options?
You probably put: "/ip route print where ?" in the console (with question mark) and you got shorter list of options:
                 whitespace
        <value>  literal value that consists only of digits, letters and 
                 characters ._
              !  logical negation
              "  start of quoted string
              $  substitution
              (  start of subexpression
              -  unary minus
              >  quote
              [  start of command substitution
              {  start of array value
              ~  bit inversion
            bgp  Find or not BGP routing protocol
  check-gateway  Whether all nexthops of this route are checking reachability 
                 of gateway by sending arp requests every 10 seconds
        comment  Short description of the item
       disabled  Defines whether item is ignored or used
       distance  Administrative distance of the route
    dst-address  Destination address
           ospf  Find or not OSPF routing protocol
            rip  Find or not RIP routing protocol
   routing-mark  It's used for policy-routing
Type in the console: "/ip route print where <tab>" (press [TAB] at the end) and you will get full list of options:
active                bgp-weight     gateway-status  routing-mark 
bgp                   blackhole      mme             scope        
bgp-as-path           check-gateway  ospf            static       
bgp-atomic-aggregate  comment        ospf-metric     target-scope 
bgp-communities       connect        ospf-type       type         
bgp-ext-communities   disabled       pref-src        unreachable  
bgp-local-pref        distance       prohibit        vrf-interface
bgp-med               dst-address    received-from   
bgp-origin            dynamic        rip             
bgp-prepend           gateway        route-tag       
Now you see options you are looking for (connect, static).
To see static routes type:
/ip route print where static=yes
To see connected routes type:
/ip route print where connect=yes
You can combine options to filter what you want:
/ip route print where static=yes and active=yes
[TAB] key will always give you list of possible options.
 
lz1dsb
Member Candidate
Member Candidate
Topic Author
Posts: 226
Joined: Wed Aug 07, 2013 11:48 am

Re: Filtering outputs on print commands? How?

Thu Aug 21, 2014 11:46 am

How can I filter the output of any print command in RouterOS?
for example: ip route print - how can I get only the connected, static, etc... it does have embedded filter for BGP and OSPf though - it works.
But how can I flexibly define what I want to be displayed? I'm running full table BGP now and it's really needed.
Is there a nice guide explaining these command options?
You probably put: "/ip route print where ?" in the console (with question mark) and you got shorter list of options:
                 whitespace
        <value>  literal value that consists only of digits, letters and 
                 characters ._
              !  logical negation
              "  start of quoted string
              $  substitution
              (  start of subexpression
              -  unary minus
              >  quote
              [  start of command substitution
              {  start of array value
              ~  bit inversion
            bgp  Find or not BGP routing protocol
  check-gateway  Whether all nexthops of this route are checking reachability 
                 of gateway by sending arp requests every 10 seconds
        comment  Short description of the item
       disabled  Defines whether item is ignored or used
       distance  Administrative distance of the route
    dst-address  Destination address
           ospf  Find or not OSPF routing protocol
            rip  Find or not RIP routing protocol
   routing-mark  It's used for policy-routing
Type in the console: "/ip route print where <tab>" (press [TAB] at the end) and you will get full list of options:
active                bgp-weight     gateway-status  routing-mark 
bgp                   blackhole      mme             scope        
bgp-as-path           check-gateway  ospf            static       
bgp-atomic-aggregate  comment        ospf-metric     target-scope 
bgp-communities       connect        ospf-type       type         
bgp-ext-communities   disabled       pref-src        unreachable  
bgp-local-pref        distance       prohibit        vrf-interface
bgp-med               dst-address    received-from   
bgp-origin            dynamic        rip             
bgp-prepend           gateway        route-tag       
Now you see options you are looking for (connect, static).
To see static routes type:
/ip route print where static=yes
To see connected routes type:
/ip route print where connect=yes
You can combine options to filter what you want:
/ip route print where static=yes and active=yes
[TAB] key will always give you list of possible options.
Thank you!
I have somehow missed the <Tab> option...
And what about the find option? Can I also use it for custom filtering? I cannot seem to get a hand of the syntax...
 
User avatar
payday
Member Candidate
Member Candidate
Posts: 233
Joined: Thu Aug 16, 2012 11:05 pm

Re: Filtering outputs on print commands? How?

Thu Aug 21, 2014 9:44 pm

Find works in the same way as print but it does not print anything and just returns IDs. It is mainly used in scripts. If you want to see results of find you have to type:
/ip route> :put [find static=yes]
*30000003
Then you can use this ID to get item's properties:
/ip route> :put [get [find static=yes] dst-address ]]
0.0.0.0/0
Another example:
/ip route>  :foreach Id in=[find dynamic=yes ] do={:put [get $Id gateway-status  ] }   
15.14.19.15 reachable via  pppoe-out1
bridge-local reachable
pppoe-out1 reachable
Have fun! :)
 
NSimpraga
newbie
Posts: 36
Joined: Fri Sep 27, 2019 2:47 pm

Re: Filtering outputs on print commands? How?

Wed Feb 05, 2020 5:47 pm

I have to revive this thread (better than opening a new one) because I have the same issue, only this doesn't work anymore.

I also need to filter the output of the '/ip firewall connection print' command to only show src-address and orig-rate&reply-rate columns.
I tried '/ip firewall connection print where src-address= yes and orig-rate=yes and reply-rate=yes' but to no avail.

Something changed in the newer RouterOS versions or have I gotten this wrong?

EDIT: I understand this is completely wrong and the where statement doesn't work this way. Is there any other way to accomplish what I need here? Filter the output to show only the columns I need?
 
User avatar
macsrwe
Forum Guru
Forum Guru
Posts: 1007
Joined: Mon Apr 02, 2007 5:43 am
Location: Arizona, USA
Contact:

Re: Filtering outputs on print commands? How?

Thu Feb 06, 2020 12:34 am

Only "outside the CLI box," viz., you can use Winbox instead and display only columns you select.
 
NSimpraga
newbie
Posts: 36
Joined: Fri Sep 27, 2019 2:47 pm

Re: Filtering outputs on print commands? How?

Thu Feb 06, 2020 11:13 am

Thing is, I am trying to make a python script that connects to the router and gets all the ip firewall connection info, but when I run the command from the python script only the first 4 columns get printed out. It's because the terminal window the script opens up isn't big enough to hold all the data so it only prints out the columns that can fit.

I either want to print out only the data I need so it fits inside that window or make the window bigger, which seems impossible. Here is the script:
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myIP', username='myUsername', password='myPass')
stdin, stdout, stderr = client.exec_command('ip firewall connection print')
for line in stdout:
    print(line.strip('\n'))
    
client.close()
I can't seem to work out any way of solving this, it seems so silly that there is no workaround.
 
SumNathan
just joined
Posts: 8
Joined: Tue May 07, 2019 2:44 am

Re: Filtering outputs on print commands? How?

Thu Jun 24, 2021 5:16 am

Thing is, I am trying to make a python script that connects to the router and gets all the ip firewall connection info, but when I run the command from the python script only the first 4 columns get printed out. It's because the terminal window the script opens up isn't big enough to hold all the data so it only prints out the columns that can fit.

I either want to print out only the data I need so it fits inside that window or make the window bigger, which seems impossible. Here is the script:
import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myIP', username='myUsername', password='myPass')
stdin, stdout, stderr = client.exec_command('ip firewall connection print')
for line in stdout:
    print(line.strip('\n'))
    
client.close()
I can't seem to work out any way of solving this, it seems so silly that there is no workaround.
I know this is an old thread - posting in case someone else finds it useful.
You could try the following in your client.exec_command statement:
/ip firewall connection print terse without-paging

Who is online

Users browsing this forum: Amazon [Bot], BartoszP, Bing [Bot], robertkjonesjr, RobertsN, VinceKalloe and 93 guests