pipe command usage

Answered Question
Sep 11th, 2007

I would like to skim through my show run command, and grep out only the "interface" and whether "bpduguard enabled" on that interface. Something like:

sh run | incl (interface | bpduguard)

thanks

jimmyc

Correct Answer by andrew.burns about 9 years 5 months ago

Hi,

It works because the second pipe is taken as an alternation character (i.e. "OR") so what happens is that for each line in the config the parser searches for either "interface" or "bpduguard" and prints out a match if it finds either one.

See the chapter "Using the IOS CLI" in the configuration fundamentals book:

http://www.cisco.com/en/US/products/ps6350/products_configuration_guide_chapter09186a008044036c.html#wp1002025

It also works for multiple pipes, e.g. "show run | include interface | duplex | speed"

HTH

Andrew.

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 3.7 (3 ratings)
Loading.
Joe Clarke Tue, 09/11/2007 - 22:35

If your device is running 12.3(2)T or higher (or 12.2(18)SXF5 or 12.2(40)SE or higher), you can use tclsh to do this. For example:

proc find_matching_ints { cmd } {

set output [exec "show run"]

set ints [list]

set currint ""

foreach line [split $output "\n"] {

if { [regexp {^interface (.*)$} $line ignore interface] } {

set currint [string trim $interface]

}

if { [regexp "^\\s+$cmd\[\\r\\n\]?$" $line] } {

lappend ints $currint

}

}

foreach int [lsort -unique $ints] {

puts $int

}

}

find_matching_ints "bpduguard enable"

Jon Marshall Tue, 09/11/2007 - 22:43

Hi Joe

This question has come up quite a few times. Do you know if Cisco have any plans to extend their regex syntax to include things such as matching across lines etc. It would be a very useful function if they did.

Alternatively it would be almost as useful if you could use multiple pipes eg

sh run | begin interface | include bpduguard

Jon

Jon Marshall Tue, 09/11/2007 - 22:52

Joe

Actually that pipe example wouldn't work but still multiple pipes would be useful for other things.

Jon

Joe Clarke Tue, 09/11/2007 - 22:56

I haven't heard of any plans to add multiple pipe support or context matching support. The thought may be that TCL is coming to all platforms eventually, and it will offer a broader flexibility.

jimmyc_2 Wed, 09/12/2007 - 04:44

I tried something that worked, I don't know how or why. Here is the exact syntax:

sh run | incl interface | bpduguard

I would have expected that I would be required to use parentheses, like my earlier example.

Version = Cat4507 12.2(25)EWA8

Correct Answer
andrew.burns Wed, 09/12/2007 - 06:07

Hi,

It works because the second pipe is taken as an alternation character (i.e. "OR") so what happens is that for each line in the config the parser searches for either "interface" or "bpduguard" and prints out a match if it finds either one.

See the chapter "Using the IOS CLI" in the configuration fundamentals book:

http://www.cisco.com/en/US/products/ps6350/products_configuration_guide_chapter09186a008044036c.html#wp1002025

It also works for multiple pipes, e.g. "show run | include interface | duplex | speed"

HTH

Andrew.

jimmyc_2 Wed, 09/12/2007 - 06:24

Hi Andrew,

I could not find that paragraph that explains where the "|" is equal to "or". Can you point me specifically to that text.

thanks.

jimmyc_2 Wed, 09/12/2007 - 06:31

Never mind, it was right there under alternation character, just like you said. Why can't Cisco use English, and say "or" instead of alternation character?!!!

Thanks for your help.

Jimmyc

Actions

This Discussion