AttributeError:'NoneType'对象没有属性'group',

问题描述 投票:0回答:1

我正在尝试通过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”。

python regex cisco netmiko
1个回答
0
投票
显然
output不包含预期的内容。没有比赛。该对象为NULL。 

首先测试比赛:

import re txt = "System image file is \"flash:/c2900-universalk9-mz.SPA.156-3.M6.bin\"" match = re.search(r'["]flash:/(.*)["]',txt) if ( match ) : print(match.group(1)) else : print("No match")
© www.soinside.com 2019 - 2024. All rights reserved.