Python Forum

Full Version: netmiko: print out specific line matches with 'Cisco IOS Software' in sh ver output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is sample output of Cisco Switch show version command.

    Switch#show version
    Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
    Technical Support: http://www.cisco.com/techsupport
    Copyright (c) 1986-2012 by Cisco Systems, Inc.
Objective: If string Cisco IOS Software is found in show version output, I would like to print the whole line.

To make it easier to understand, let me put show version output in variable shvar

    shvar = '''
    Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
    Technical Support: http://www.cisco.com/techsupport
    Copyright (c) 1986-2012 by Cisco Systems, Inc.
    '''
Search with if

    >>> if 'Cisco IOS Software' in shvar:
    ...     print('Found ... print line')
    ... 
    Found ... print line
    >>> 
Or Search with find

    >>> if shvar.find('Cisco IOS Software') > 0:
    ...     print('Found ... print line')
    ... 
    Found ... print line
    >>> 
The question is how do I print the line matches with 'Cisco IOS Software'?

Desired Output

    Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
One option is to do : show version | incl IOS Software instead of just show version.
Then you are sure you only get that line..

Another option is using regex. I got an example on that specific somewhere, just cant find it right now. :(

/ carsten

(Apr-20-2020, 05:52 AM)sabrina Wrote: [ -> ]This is sample output of Cisco Switch show version command.

    Switch#show version
    Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
    Technical Support: http://www.cisco.com/techsupport
    Copyright (c) 1986-2012 by Cisco Systems, Inc.
Objective: If string Cisco IOS Software is found in show version output, I would like to print the whole line.

To make it easier to understand, let me put show version output in variable shvar

    shvar = '''
    Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
    Technical Support: http://www.cisco.com/techsupport
    Copyright (c) 1986-2012 by Cisco Systems, Inc.
    '''
Search with if

    >>> if 'Cisco IOS Software' in shvar:
    ...     print('Found ... print line')
    ... 
    Found ... print line
    >>> 
Or Search with find

    >>> if shvar.find('Cisco IOS Software') > 0:
    ...     print('Found ... print line')
    ... 
    Found ... print line
    >>> 
The question is how do I print the line matches with 'Cisco IOS Software'?

Desired Output

    Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)