Back to the Basics – Pipe Dreams

Standard

To finish off our fun with show commands series, likes look at one of my favourite tools, the pipe command!

Just like with Windows or Linux we can use the | to manipulate show command output to do various things.

To start out lets see what interfaces are on this router.

R01#show ip int br                
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                192.168.136.162 YES DHCP   up                    up      
Ethernet0/1                10.1.2.1        YES manual up                    up      
Ethernet0/2                unassigned      YES NVRAM  administratively down down    
Ethernet0/3                unassigned      YES NVRAM  administratively down down    
Ethernet1/0                unassigned      YES NVRAM  administratively down down    
Ethernet1/1                unassigned      YES NVRAM  administratively down down    
Ethernet1/2                unassigned      YES NVRAM  administratively down down    
Ethernet1/3                unassigned      YES NVRAM  administratively down down    
Serial2/0                  unassigned      YES NVRAM  administratively down down    
Serial2/1                  unassigned      YES NVRAM  administratively down down    
Serial2/2                  unassigned      YES NVRAM  administratively down down    
Serial2/3                  unassigned      YES NVRAM  administratively down down    
Serial3/0                  unassigned      YES NVRAM  administratively down down    
Serial3/1                  unassigned      YES NVRAM  administratively down down    
Serial3/2                  unassigned      YES NVRAM  administratively down down    
Serial3/3                  unassigned      YES NVRAM  administratively down down
Loopback0                  192.168.10.1    YES manual up                    up      
Loopback1                  192.168.11.1    YES manual up                    up      
Loopback2                  192.168.12.1    YES manual up                    up      
Loopback3                  192.168.13.1    YES manual up                    up      
Loopback4                  192.168.14.1    YES manual up                    up      
Loopback5                  192.168.15.1    YES manual up                    up      
Loopback6                  192.168.16.1    YES manual up                    up      
Loopback7                  192.168.17.1    YES manual up                    up      
Loopback8                  192.168.18.1    YES manual up                    up      
Loopback9                  192.168.19.1    YES manual up                    up

We can use the begin keyword to start the configuration at a certain point, it is easy to use but can be a little bit limiting with long configs.

R01#show run | begin line
line con 0
 exec-timeout 0 0
 privilege level 15
 logging synchronous
line aux 0
 exec-timeout 0 0
 privilege level 15
 logging synchronous
line vty 0 4
 login
 transport input none
!
!
end

We can use the include pipe to display only lines that match the include statment, for example if we just wanted to see the Ethernet interfaces in this router we can do a:

R01#show ip int br | include Ethernet
Ethernet0/0                192.168.136.162 YES DHCP   up                    up      
Ethernet0/1                10.1.2.1        YES manual up                    up      
Ethernet0/2                unassigned      YES NVRAM  administratively down down    
Ethernet0/3                unassigned      YES NVRAM  administratively down down    
Ethernet1/0                unassigned      YES NVRAM  administratively down down    
Ethernet1/1                unassigned      YES NVRAM  administratively down down    
Ethernet1/2                unassigned      YES NVRAM  administratively down down    
Ethernet1/3                unassigned      YES NVRAM  administratively down down 

The exclude does the opposite and shows everything that doesn’t match, one of the more useful examples of this is to only show interfaces with an IP assigned.

R01#show ip int br | exclude unass
Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                192.168.136.162 YES DHCP   up                    up      
Ethernet0/1                10.1.2.1        YES manual up                    up 

We won’t get too much into it today but Cisco supports fairly complex regular expresssions to give you more control. In this example we are including interfaces that start with Ethernet but are also up/up.

R01#show run | in Ether.*up   
R01#show ip int br | in Ethernet.*up
Ethernet0/0                192.168.136.162 YES DHCP   up                    up      
Ethernet0/1                10.1.2.1        YES manual up                    up

In this one we are using a range to show just interfaces with an IP between 14 and 16 in the third octet.

R01#show ip int br | in \.1[4-6]\.1
Loopback4              192.168.14.1    YES manual up                    up      
Loopback5              192.168.15.1    YES manual up                    up      
Loopback6              192.168.16.1    YES manual up                    up

We can also use a second pipe in our statement to create a logical OR. This can help us control output in interesting ways, for example we can use include interface to grab all the interface names and then pipe in another include ip address to add the IP address underneath. You can have as many ORs as you want but you can’t change the pipe type, so you can’t do an include and then try a exclude.

R01#show running-config | in interface|ip address
interface Loopback0
 ip address 192.168.10.1 255.255.255.0
interface Loopback1
 ip address 192.168.11.1 255.255.255.0
interface Loopback2
 ip address 192.168.12.1 255.255.255.0
interface Loopback3
 ip address 192.168.13.1 255.255.255.0
interface Loopback4
 ip address 192.168.14.1 255.255.255.0
interface Loopback5
 ip address 192.168.15.1 255.255.255.0
interface Loopback6
 ip address 192.168.16.1 255.255.255.0
interface Loopback7
 ip address 192.168.17.1 255.255.255.0
interface Loopback8
 ip address 192.168.18.1 255.255.255.0
interface Loopback9
 ip address 192.168.19.1 255.255.255.0

The section command is one of the most powerful filters, it can show an entire config stanza of what you are looking for.

R01#show run | section router
router eigrp IWAN
 !
 address-family ipv4 unicast autonomous-system 123
  !
  topology base
  exit-af-topology
  network 10.0.0.0
  network 10.1.3.0 0.0.0.255
 exit-address-family
router ospf 1
 router-id 1.1.1.1
 network 10.1.2.0 0.0.0.255 area 0
router rip
 version 2
 network 10.0.0.0
 no auto-summary
router bgp 200
 bgp log-neighbor-changes
 network 10.0.0.0
 neighbor 10.10.2.203 remote-as 100

It can be filter down to a particular protocol as well.

R01#show run | section router eigrp
router eigrp IWAN
 !
 address-family ipv4 unicast autonomous-system 123
  !
  topology base
  exit-af-topology
  network 10.0.0.0
  network 10.1.3.0 0.0.0.255
 exit-address-family

R01#show run | section interface
interface Loopback0
 description Loopback
 ip address 192.168.10.1 255.255.255.0
interface Loopback1
 ip address 192.168.11.1 255.255.255.0
interface Loopback2
 ip address 192.168.12.1 255.255.255.0
interface Loopback3
 ip address 192.168.13.1 255.255.255.0
interface Loopback4
 ip address 192.168.14.1 255.255.255.0
interface Loopback5
 ip address 192.168.15.1 255.255.255.0
interface Loopback6
 ip address 192.168.16.1 255.255.255.0
interface Loopback7
 ip address 192.168.17.1 255.255.255.0
interface Loopback8
 ip address 192.168.18.1 255.255.255.0
interface Loopback9
 description Loopback
 ip address 192.168.19.1 255.255.255.0
interface Ethernet0/0
 ip address dhcp
interface Ethernet0/1
 ip address 10.1.2.1 255.255.255.0
interface Ethernet0/2
 ip address 10.11.22.11 255.255.255.0
interface Ethernet0/3
 ip address 10.111.222.111 255.255.255.0
interface Ethernet1/0
 ip address 10.1.11.1 255.255.255.0

The pipe isn’t all about filtering output, it can also be used to redirect output to somewhere else, in this example I’m sending the output of show ip route to a TFTP server. This can be handy if you wanted to capture select output before doing a change etc. The redirect command just sends the output and doesn’t show it on the screen, the tee command does both.

R01#show ip route | redirect tftp://10.10.13.13/routes.txt
!
R01#
R01#show ip route | tee tftp://10.10.13.13/routes.txt     
!
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area 
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override

Gateway of last resort is 192.168.136.2 to network 0.0.0.0

S*    0.0.0.0/0 [254/0] via 192.168.136.2
      192.168.136.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.136.0/24 is directly connected, Ethernet0/0
L        192.168.136.162/32 is directly connected, Ethernet0/0

R01#show ip route | tee tftp://10.10.13.13/routes.txt

The pipes available to you will vary from platform to platform, for example an ASA doesn’t have the section command because it doesn’t need one since all it has been moved to the show run command, it does however have a direct grep command.

ASA01(config)# show run | ?

  begin    Begin with the line that matches
  exclude  Exclude lines that match
  grep     Include/exclude lines that match
  include  Include lines that match

The XR platform uses more direct linux utilities to do the job

RP/0/0/CPU0:ios#show run | ?
  begin    Begin with the line that matches
  exclude  Exclude lines that match
  file     Save the configuration
  include  Include lines that match
  utility  A set of common unix utilities
  <cr>     Shows current operating configuration
RP/0/0/CPU0:ios#show run | utility ?
  cut     Cut out selected fields of each line of a file
  egrep   Extended regular expression grep
  fgrep   Fixed string expression grep
  head    Show set of lines/characters from the top of a file
  less    Fixed string pattern matching
  more    Paging Utility More
  script  Launch a script for post processing
  sort    Sort, merge, or sequence-check text files
  tail    Copy the last part of files
  uniq    Report or filter out repeated lines in a file
  wc      Counting lines/words/characters of a file
  xargs   Construct argument list(s) and invoke a program

And the Nexus has a pretty good mix between the two

NX9K01# show run | ?
  awk          Mini AWK
  cut          Print selected parts of lines.
  diff         Show difference between current and previous invocation (creates
               temp files: remove them with 'diff-clean' command and dont use 
               it on commands with big outputs, like 'show tech'!) 
  egrep        Egrep - print lines matching a pattern
  email        Email command output
  grep         Grep - print lines matching a pattern
  head         Display first lines
  human        Output in human format
  json         Output in json format
  json-pretty  Output in json pretty print format
  last         Display last lines
  less         Filter for paging
  no-more      Turn-off pagination for command output
  section      Show lines that include the pattern as well as the subsequent
               lines that are more indented than matching line 
  sed          Stream Editor
  sort         Stream Sorter
  tr           Translate, squeeze, and/or delete characters
  uniq         Discard all but one of successive identical lines
  vsh          The shell that understands cli command
  wc           Count words, lines, characters
  xml          Output in xml format (according to .xsd definitions)
  xmlin        Convert CLI show commands to their XML formats
  xmlout       Output in xml format (according to the latest .xsd version)
  begin        Begin with the line that matches
  count        Count number of lines
  end          End with the line that matches
  exclude      Exclude lines that match
  include      Include lines that match

If you want more flexiblity you can enable the Cisco bash shell so you can use tools like grep or cut to further filter output. I did a Bashing Cisco blog post awhile back if you want more detail, to end things here is a grep example that matches 192 IPs, excludes IPs with a 0 or is between 7-9 and also excludes 136.

R01#   terminal shell 
R01#$ br | grep 192 | grep -v 192.168.1[0,7-9]\.1 | grep -v 192.168.136
Loopback1                  192.168.11.1    YES manual up                    up      
Loopback2                  192.168.12.1    YES manual up                    up      
Loopback3                  192.168.13.1    YES manual up                    up      
Loopback4                  192.168.14.1    YES manual up                    up      
Loopback5                  192.168.15.1    YES manual up                    up      
Loopback6                  192.168.16.1    YES manual up                    up 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s