Can I match on a search string and show the lines above it in IOS?
FrankGuthrie
Member Posts: 245
in CCNP
For example, I have a MAC address which i know is the MAC address on one of the interfaces. However when I search to what interface the MAC address belongs, it does not show me to what interface it belongs as this is on a separate line.
When using grep to go through a router config I can use grep -5 <MAC ADDRESS> to show 5 lines above and 5 lines below the line on which the search string is found. How ever MAC addresses are not in the running config, so is this possible?
A sort of workaround I use now is use sh interaces | begin <MAC ADDRESS>
If I know the interface below I know what interface the MAC address belongs to.
So is there an equivalent of grep -5 <MAC ADDRESS>?
When using grep to go through a router config I can use grep -5 <MAC ADDRESS> to show 5 lines above and 5 lines below the line on which the search string is found. How ever MAC addresses are not in the running config, so is this possible?
A sort of workaround I use now is use sh interaces | begin <MAC ADDRESS>
If I know the interface below I know what interface the MAC address belongs to.
So is there an equivalent of grep -5 <MAC ADDRESS>?
Comments
-
thomas_ Member Posts: 1,012 ■■■■■■■■□□Have you tried:
sh interfaces | i interface|H.H.H
If you don't put any spaces between the second pipe it treats it like a logical OR statement. On second thought you might get a lot of extra interface statements with that.
If your IOS version supports it you can do a "terminal shell" in enable mode or "shell processing full" in global configuration mode and you'll have access to a grep command, but I don't think it has the capability to show five lines back and five lines forward like you want. -
thomas_ Member Posts: 1,012 ■■■■■■■■□□You might also try doing a show run and then typing
/H.H.H
where H.H.H is the mac address and press enter if needed, but you'll probably have the same issue with it now showing the interface. -
thomas_ Member Posts: 1,012 ■■■■■■■■□□I figured it out, but you need an IOS version that has IOS.sh:
term shell
sh ru | grep -u H.H.H | tail 10
The -u flag will show all of the lines above the mac address you're searching and then the tail command will just show the last ten lines of that output. You can substitute "10" with however many lines you want. -
negru_tudor Member Posts: 473 ■■■□□□□□□□I figured it out, but you need an IOS version that has IOS.sh:
term shell
sh ru | grep -u H.H.H | tail 10
The -u flag will show all of the lines above the mac address you're searching and then the tail command will just show the last ten lines of that output. You can substitute "10" with however many lines you want.
wow. nice find!2017-2018 goals:
[X] CIPTV2 300-075
[ ] SIP School SSCA
[X] CCNP Switch 300-115 [X] CCNP Route 300-101 [X] CCNP Tshoot 300-135
[ ] LPIC1-101 [ ] LPIC1-102 (wishful thinking) -
FrankGuthrie Member Posts: 245Excellent find, but how do I know I have the IOS.sh?
EDIT/UPDATE:
Found this:
http://www.cisco.com/c/en/us/td/docs/ios/netmgmt/configuration/guide/Convert/IOS_Shell/nm_ios_shell.pdf
Prerequisites for Cisco IOS.sh
• Cisco IOS Release 15.1(4)M, 15.1(2)S, and later releases.
• Cisco IOS.sh must be configured and enabled to use the Cisco IOS.sh features and functions on yourrouter.