Using the Pipe in IOS
A lot of IOS commands give you a lot of information. Most of the time, though, it’s way too much information, and it sure would be nice to do some grep-like stuff on the output, right? Well, just like on Linux, you can use the pipe (|) to do such. That’s not a butt cheek, by the way.
The most useful function is the include directive. This is the equivalent of just plain grep on Linux, and will show you only lines that match a string that you give it. Say that you want to find what ports on your switch are down, but don’t want to grind through all the lines of a show ip interface brief. If you just pipe it to the include command followed by the word “down”, you’ll see something like this.
Switch#show ip interface brief | include down
GigabitEthernet0/4 unassigned YES unset down down
GigabitEthernet0/7 unassigned YES unset down down
GigabitEthernet0/17 unassigned YES unset down down
GigabitEthernet0/18 unassigned YES unset down down
GigabitEthernet0/19 unassigned YES unset down down
GigabitEthernet0/20 unassigned YES unset down down
You can also use the exclude directive, which is the same as a grep -v on Linux. I hope you figured out that this gives you all lines that don’t match the word, so, let’s use the exclude directive to found out what ports are down. How about we just ignore the lines that are up.
Switch#show ip interface brief | exclude up
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/4 unassigned YES unset down down
GigabitEthernet0/7 unassigned YES unset down down
GigabitEthernet0/17 unassigned YES unset down down
GigabitEthernet0/18 unassigned YES unset down down
GigabitEthernet0/19 unassigned YES unset down down
GigabitEthernet0/20 unassigned YES unset down down
Well, this won’t exactly give you all the ports that are down. What if the port is up/down?
What else can you use with the pipe? What if you want to look at the configurations of all the ports or interfaces on a box but don’t want to go through the config hitting the spacebar over and over. If you use the begin command, you’ll see the output beginning from the first match, so, using the string interface will show you the config starting at the first interface.
Switch#show running-config | begin interface
interface GigabitEthernet0/1
description Server
switchport access vlan 4
switchport mode access
spanning-tree portfast…
Another good one is the section command. It’s usually used on the output of show running-config and shows you sections of the config that match your string. Huh? If you want to see the BGP section of the configuration, you can do something like thing just to see that part of the configuration.
Router#show running-config | section bgp
router bgp 1
neighbor 1.1.1.1 remote-as 65000
neighbor 1.1.1.1 version 4
There are a few other commands for use with the pipe, so explore on your own. You might also want to check out regular expressions on the Cisco IOS if you want to match more than just simple text.
on April 14th, 2008 at 9:03 am
Great article! Keep it up!
You can just add this tidbit to your article and not post the comment, but I just found that you can use an OR statement the other day. Just enclose the search items in parenthesis and separate them with pipes.
bunker-3825-r1#show interface | i (line protocol|error|rate)
GigabitEthernet0/0 is up, line protocol is up
Queueing strategy: fifo
5 minute input rate 4882000 bits/sec, 1425 packets/sec
5 minute output rate 909000 bits/sec, 1188 packets/sec
2 input errors, 0 CRC, 0 frame, 2 overrun, 0 ignored
0 output errors, 0 collisions, 0 interface resets
on April 14th, 2008 at 9:18 am
Absolutely right, Josh. That’s part of the regular expression syntax for IOS. You can do all sorts of stuff, too. You can match only interfaces F0/1, F0/2, F0/7 with this.
…. | include 0/[127]
Or to find all the IP addresses in the config, do this.
sh run | incl address [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+
I’ve gone back and set a link to Cisco page on regular expressions, so check it out.
on April 14th, 2008 at 11:11 pm
Thanks for adding the link.
on April 15th, 2008 at 11:20 pm
Dang, while I have always used the include and begin, etc. I wasn’t aware I could use it to this extent. Time to start customizing some Cisco scripts on my Unix box now to pull more specific info! Great article!!
on April 16th, 2008 at 1:00 pm
Thanks, Clint. Regular expressions are crazy and can do amazing things. They’re used elsewhere in IOS (like BGP AS path stuff), so it’s worth checking out.
on April 21st, 2008 at 6:03 am
I don’t know what I’d do without “include” or “begin” .. Now I’ve just added “section”
Thanks!