我正在尝试通过 Netmiko 获取 cisco 版本。
import re
from netmiko import ConnectHandler
iosv_l3 = {
'device_type': 'cisco_ios',
'ip': 'my ip',
'username': 'username',
'password': 'password',
'secret': 'enable password'
}
net_connect = ConnectHandler(**iosv_l3)
net_connect.enable()
output = net_connect.send_command('show version | include flash')
print(output)
x = re.search(r'["]flash:/(.*)["]',output).group(1)
print(x)
net_connect.disconnect()
Netmiko 可以成功地通过 SSH 连接到 Cisco 设备。我可以看到 print(output) 的输出:
System image file is "flash:c2900-universalk9-mz.SPA.156-3.M6.bin"
但是,代码会导致错误:
x = re.search(r'["]flash:/(.*)["]',output).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
我创建了一个测试文件来测试正则表达式:
import re
txt = "System image file is \"flash:/c2900-universalk9-mz.SPA.156-3.M6.bin\""
txt = re.search(r'["]flash:/(.*)["]',txt).group(1)
print(txt)
测试正确打印“c2900-universalk9-mz.SPA.156-3.M6.bin”。