cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
995
Views
5
Helpful
1
Replies

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

write_erase
Level 1
Level 1

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 `shvar` variable

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.
'''

There are 2 ways that I know of to find something ...
1. Search with `if`

>>> if 'Cisco IOS Software' in shvar:
... print('Found ... print line')
...
Found ... print line
>>>

Or

2. 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' instead of just showing "Found ... print line" output?

Desired Output

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

 

1 Accepted Solution

Accepted Solutions

omz
VIP Alumni
VIP Alumni

Hi 

You could do something like this - 

#!/usr/bin/env python
from netmiko import ConnectHandler
import re
cisco_881 = {
'device_type': 'cisco_ios',
'host': '192.168.106.153',
'username': 'cisco',
'password': 'cisco',
'port': '22'
}

net_connect = ConnectHandler(**cisco_881)
output = net_connect.send_command('',expect_string=r'#',strip_command=False, strip_prompt=False)
output += net_connect.send_command('show version',expect_string=r'#',strip_command=False, strip_prompt=False)
net_connect.disconnect()

IOS_RE = r'(Cisco IOS\s+(\S+\s+.*))'        
matched = re.findall(IOS_RE,output)
print(matched[0][0])

Output

Screenshot 2020-04-20 at 09.07.20.png

 

 

View solution in original post

1 Reply 1

omz
VIP Alumni
VIP Alumni

Hi 

You could do something like this - 

#!/usr/bin/env python
from netmiko import ConnectHandler
import re
cisco_881 = {
'device_type': 'cisco_ios',
'host': '192.168.106.153',
'username': 'cisco',
'password': 'cisco',
'port': '22'
}

net_connect = ConnectHandler(**cisco_881)
output = net_connect.send_command('',expect_string=r'#',strip_command=False, strip_prompt=False)
output += net_connect.send_command('show version',expect_string=r'#',strip_command=False, strip_prompt=False)
net_connect.disconnect()

IOS_RE = r'(Cisco IOS\s+(\S+\s+.*))'        
matched = re.findall(IOS_RE,output)
print(matched[0][0])

Output

Screenshot 2020-04-20 at 09.07.20.png

 

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: