Python netmiko:如何在“show version”命令中打印与“Cisco IOS软件”匹配的特定行?

2024-09-30 16:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这是Cisco Switch show version命令的示例输出

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.

目标:如果在“show version”输出中找到字符串Cisco IOS Software,我想打印整行

为了更容易理解,让我把show version输出放在变量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.
'''

if搜索

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

或使用find进行搜索

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

问题是如何打印与“Cisco IOS软件”匹配的行

期望输出

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

Tags: releaseversionshowlinesoftwareciscoiosprint