+ Reply to Thread
Results 1 to 6 of 6
  1. Senior Member
    Join Date
    Nov 2012
    Posts
    242
    #1

    Default Can I match on a search string and show the lines above it in IOS?

    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>?
    Reply With Quote Quote  

  2. SS -->
  3. Senior Member
    Join Date
    Apr 2015
    Posts
    717

    Certifications
    CCNA R&S, Security+, Network+, Linux Essentials
    #2
    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.
    Reply With Quote Quote  

  4. Senior Member
    Join Date
    Apr 2015
    Posts
    717

    Certifications
    CCNA R&S, Security+, Network+, Linux Essentials
    #3
    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.
    Reply With Quote Quote  

  5. Senior Member
    Join Date
    Apr 2015
    Posts
    717

    Certifications
    CCNA R&S, Security+, Network+, Linux Essentials
    #4
    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.
    Reply With Quote Quote  

  6. Senior Member
    Join Date
    Jan 2015
    Location
    Cluj-Napoca, RO
    Posts
    326

    Certifications
    Several (ITIL, Avaya, ShoreTel, Cisco)
    #5
    Quote Originally Posted by thomas_ View Post
    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:
    [ ] CIPTV2 300-075
    [ ] SIP School SSCA
    [X] CCNP Switch 300-115 [ ] CCNP Route 300-101 [ ] CCNP Tshoot 300-135
    [ ] LPIC1-101 [ ] LPIC1-102 (wishful thinking)
    Reply With Quote Quote  

  7. Senior Member
    Join Date
    Nov 2012
    Posts
    242
    #6
    Excellent 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_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.
    Last edited by FrankGuthrie; 12-18-2016 at 10:29 AM.
    Reply With Quote Quote  

+ Reply to Thread

Social Networking & Bookmarks