Search code examples
pythonpython-3.xcisconetmiko

Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command?


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 the '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)

Solution

  • You could just split the string into lines.

    for line in shvar.split("\n"):
       if 'Cisco IOS Software' in line:
          print(line)