Filtering a command output

Hello,

I never tried MT scripting so I’m having a little trouble to make this work…

Consider the command “ppp active print detail without-paging”… Let’s say I need to filter its output to only show me the lines where the usernames start with the letter “a”…

How can I do that?

Thanks in advance.

The following command if pasted into the terminal will print all ppp active users having names starting from “ex”:

:global list “”; :foreach i in [/ppp active find] do={:if ([:find [/ppp active get $i name] “ex”]=0) do={:set list ($list . “,” . $i);}}; /ppp active print detail without-paging from $list;


Eugene

I would point this out as another example of why something like ‘grep’ would be a wonderful addition.

/ppp active print detail without-paging | grep ex

would be much quicker, and so much more obvious to many people.

–Eric

How about this: “It’s inherently fun to program” :slight_smile:

True, and I don’t have much of a problem composing cryptic statements, but the same cannot be said for everyone.

There are many times when our tech support group has expressed frustration at the difficulty of searching in RouterOS. I usually end up telling them to use Winbox, sort on the column of their choice, and scroll through until they find what they are looking for. It’s inefficient, but it’s easier than trying to debug long commands (especially when ROS doesn’t really have any debugging tools), and much easier than trying to explain the complexities of ROS syntax to a first-year collage student answering phones.

On the other hand, with the vendor C gear, I can simply tell them to do a ‘sh ip arp | incl 0c13’, for example. Simple, and concrete. There just isn’t an equivalent in ROS that can be handled by a $9/hr tech. In general, MT gear is much more user friendly than vendor C’s, but in this particular area, MT is lacking.

Even I would often rather type 15 characters and be done with it, rather than stretching the command out to 100+. It would also be quite nice to have regular expressions available, for those more complex searches.

Once the API is released, this will likely become moot, since we will be more easily able to create our own interfaces (yes, it can be done now with SSH and Expect, but that gets has it’s own problems). But at this time, the lack of an easy search function is one of the biggest frustrations we have with MT.

–Eric

well, if you would use ssh key authentication, than you could get easy search in ROS:

ssh 10.5.8.1 /ppp active print | grep -E “Your regular expression goes here”

Eugene

Yes… I’m aware of grep and that’s what I’m using for now… But what eflanery said is true, a command inside MT would be alot more useful… And I would go a little more far than that… Not a grep, but something that would make the “ppp active print” list only what u ask so it wouldn’t have all the stress of going on 1000 users to print just 2 of them…

Sorry about any grammar mistakes… English isn’t my first language…

And, if you want to do it with a live session, with telnet, or ssh without key-auth, you can do something (with a pair of terminals) like:

Terminal 1: mkfifo fifo; cat fifo | grep -E “regex of choice”

Terminal 2: script -f fifo

Then in terminal 2, connect to the MT, do your thing, and watch for the filtered results in terminal 1.

I’m not saying there aren’t ways to get it done, there are many. I’m saying there is nothing quick and easy built-in, and that leads to frustration.

–Eric

Flip, would love to have grep.
I am an experianced linux/unix admin and infrastructure expert, not not a bloody programmer!

I normally work this stuff out after few hours, but is such a waste of time!

Hope fully someone can help!

Im trying to overcome yet another bug in MT.

it seems that my web-proxy often stops working.

it would be nice if i could do a “ip proxy monitor once” and filter for status to restart it eventually!

Tried modifying above code to …

:global list ""; :foreach i in [/ip proxy monitor once find] do={:if ([:find [/ppp active get $i name] "status"]=0) do={:set list ($list . "," . $i);}}; /ip proxy monitor once from $list;

Didn’t work
No idea how im supposed to do this.

[/ip proxy monitor once find] will not work because there is nothing to find.

How would i specify what to find/filter?

Just what you want to achieve by using this command?

[/ip proxy monitor once find]

This is completely wrong and I can’t figure out what exactly you want to find.

I would like to find the status of the web-proxy.

ie, to see if it’s state is “running” or “stopped”

Thank You.

/ip porxy monitor once do={ 
     :if ( $state = "running" ) do={
          :log info "Proxy is running";
     }
  }

That is exactly what i wan’t to do!!!

However i tried to use that code and nothing happens. :confused:

I open log to watch for output. I check that proxy is indeed running.

I create it in scheduler with 1 second interval, no output.
Tried in scripts aswell, obviously running it manually there.

thank You so much for input so far!
I have been desperate to solve this as web-proxy keeps crashing and i need something to start it up again.

I want to print in consle dhch-server leases, that belongs to network 192.168.106.0/24
In console i run

ip dhcp-server lease print where address in 192.168.106.0/24

and it don`t give me any output.
What am i doing wrong?

Can anybody help me?

Maybe the following
/ppp active print detail without-paging where user~“ex”

P.S.:
Old thread but one of the first one to be shown on google for grap like answer.

Old thread but did anything like piping and/or grep every make it into the terminal. I’d love to be able to do:

/export | grep frodo
/export | grep 192.168.88

I know it’s not much of a hassle to export to a file and download to a PC but sometimes it would be very handy.

I was looking for all IPs starting with 208…. in my arp table, and this is handy:
/ip/arp/print where address~"208"

The ‘where’ is like grep and you can specify which field… here is an example to grep on mac:
/ip/arp/print where mac-address ~ "48:8F"