04-19-2020 11:01 PM
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)
Solved! Go to Solution.
04-20-2020 12:54 AM - edited 04-20-2020 01:08 AM
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
04-20-2020 12:54 AM - edited 04-20-2020 01:08 AM
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
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: