Pipe dat Output bruh!
Who wants to trawl through thousands and thousands of lines of code looking for something......not me, pipe it!
Options
So these options will change with the platform you are working on, for IOS you get;
CLV-HO-ADM-1-SW-03#sh run | ?
append Append redirected output to URL (URLs supporting append operation only)
begin Begin with the line that matches
count Count number of lines which match regexp
exclude Exclude lines that match
format Format the output using the specified spec file
include Include lines that match
redirect Redirect output to URL
section Filter a section of output
tee Copy output to URL
Pretty Self explanatory
Double Pipe
- even though the CLI doesn't show you, you CAN double pipe!
Lets start with a simple show and pipe;
CLV-HO-ADM-1-SW-03#sh run | inc ip
no ip source-route
ip dhcp bootp ignore
ip domain-name rccprd.redland.qld.gov.au
ip name-server 10.50.191.3
ip name-server 10.50.191.131
no ip address
no ip address
ip address 10.2.0.6 255.255.255.0
ip default-gateway 10.2.0.254
no ip http server
no ip http secure-server
ip ssh time-out 10
ip ssh source-interface Vlan4000
ip ssh version 2
ip access-list standard SNMP-SERVERS
CLV-HO-ADM-1-SW-03#sh run | inc ip
no ip source-route
ip dhcp bootp ignore
ip domain-name rccprd.redland.qld.gov.au
ip name-server 10.50.191.3
ip name-server 10.50.191.131
description LACP to HO HP Core
description [Not In Use]
no ip address
description User Port
description User Port
description User Port
description User Port
description User Port
description User Port | Councillor Div 1
description User Port
description User Port | Councillor Div 8
description User Port
description User Port | Councillor Div 10
description User Port
description User Port
description User Port
description User Port
description User Port
description User Port
But maybe you don't want "description User Port" in the output
CLV-HO-ADM-1-SW-03#sh run | include ip | exclude description
no ip source-route
ip dhcp bootp ignore
ip domain-name rccprd.redland.qld.gov.au
ip name-server 10.50.191.3
ip name-server 10.50.191.131
no ip address
no ip address
ip address 10.2.0.6 255.255.255.0
ip default-gateway 10.2.0.254
no ip http server
no ip http secure-server
ip ssh time-out 10
ip ssh source-interface Vlan4000
ip ssh version 2
ip access-list standard SNMP-SERVERS
CLV-HO-ADM-1-SW-03#
AND statement
if you want to only filter output with two variables in the same line us the .*(use like the 'and' operator)
The below will display only lines with 'ip' AND 'ssh' in them;
CLV-HO-ADM-1-SW-03#sh run | include ip.*ssh
ip ssh time-out 10
ip ssh source-interface Vlan4000
ip ssh version 2
CLV-HO-ADM-1-SW-03#
The below argument will only display lines with 'Line' and '2/0/11' and 'down' in them;
CLV-HO-LIB-LG-SW-01#sh logg | inc Line.*2/0/11.*down
Dec 13 07:11:42.962 AEST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/11, changed state to down
Dec 13 08:17:14.048 AEST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/11, changed state to down
Dec 13 08:17:19.931 AEST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/11, changed state to down
Dec 13 08:18:52.713 AEST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/11, changed state to down
Dec 13 08:21:18.955 AEST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/11, changed state to down
Dec 13 08:21:24.876 AEST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/11, changed state to down
OR statement
Sometimes you will want to see this OR that on any lines...use a close | command (spacing is important)
CLV-HO-ADM-1-SW-03#sh run | include interface|access | exclude mode
interface Port-channel1
interface FastEthernet0
interface GigabitEthernet1/0/1
switchport access vlan 112
interface GigabitEthernet1/0/2
switchport access vlan 112
interface GigabitEthernet1/0/3
switchport access vlan 112
interface GigabitEthernet1/0/4
switchport access vlan 112
interface GigabitEthernet1/0/5
https://cciernstricks.com/cisco-pipe-command-examples-with-regular-expressions/
COUNT the output!
The below will count how many times the regex '2\/0\/11.*down' appears in the 'sh logg' output
CLV-HO-LIB-LG-SW-01#sh logg | count 2\/0\/11.*down
Number of lines which match regexp = 73
CLV-HO-LIB-LG-SW-01#
CLV-HO-LIB-LG-SW-01#
Redirect the output
redirect the output to a FTP/SCP/TFTP server;
CLV-HO-LIB-LG-SW-02#sh ver | redirect ftp://admin:admin@10.10.120.86/sh_ver.txt
Writing sh_ver.txt
CLV-HO-LIB-LG-SW-02#