简体   繁体   English

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

[英]AttributeError: 'NoneType' object has no attribute 'group' ,

I am trying to fetch the cisco version by Netmiko.我正在尝试通过 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()

The Netmiko can SSH to Cisco equipment successfully. Netmiko 可以成功通过 SSH 连接到 Cisco 设备。 I can see the output from print(output):我可以看到 print(output) 的输出:

System image file is "flash:c2900-universalk9-mz.SPA.156-3.M6.bin"

However, the code results in an error:但是,代码导致错误:

x = re.search(r'["]flash:/(.*)["]',output).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

I created a test file to test regex:我创建了一个测试文件来测试正则表达式:

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)

The test print "c2900-universalk9-mz.SPA.156-3.M6.bin" correctly.测试正确打印“c2900-universalk9-mz.SPA.156-3.M6.bin”。

>>> import re
>>> txt = "System image file is \"flash:/c2900-universalk9-mz.SPA.156-3.M6.bin"
>>> txt = re.search(r'["]flash:/(.*)["]',txt).group(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> print(txt)
System image file is "flash:/c2900-universalk9-mz.SPA.156-3.M6.bin
>>>
>>>

Apparently output does not contain what is anticipated.显然output不包含预期的内容。
There was no match.没有比赛。 The object is a NULL one.该对象是一个 NULL 对象。

Test the match first :首先测试匹配:

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")

The method re.match(..) returns Match object (which has .group(x) methods and etc) or None in case if the match was not found.方法re.match(..)返回Match对象(它具有.group(x)方法等)或None如果找不到匹配项。 In your case the error means that was returned None ;)在您的情况下,错误意味着返回None ;)

Ok, it means that regex pattern doesn't work for the data tested.好的,这意味着正则表达式模式不适用于测试的数据。 I've debugged your both cases and I've been noticed that in the first script you apply the pattern to is "flash:c2900- but in the second example you are testing the regex against file is \\"flash:/c2900 where between flash: and c2900 we have an extra / which doesn't exist in the first example.我已经调试了你的两种情况,我注意到在第一个脚本中你应用的模式is "flash:c2900-但在第二个例子中你正在测试针对file is \\"flash:/c2900的正则表达式file is \\"flash:/c2900其中flash:c2900我们有一个额外的/在第一个例子中不存在。

Ok, so there are 2 ways to fix it - if you want to work it with and without / using the same regex, it will be this way好的,所以有 2 种方法可以解决它 - 如果您想使用和不使用/使用相同的正则表达式来处理它,那就是这种方式

import re

output = 'System image file is "flash:c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:/?(.*)"', output).group(1))

output = 'System image file is "flash:/c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:/?(.*)"', output).group(1))

using optional regex matching ( ? ).使用可选的正则表达式匹配 ( ? )。

If you want to work only with / or without you can use these examples.如果您只想使用/或不使用,您可以使用这些示例。

import re

output = 'System image file is "flash:c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:(.*)"', output).group(1))

output = 'System image file is "flash:/c2900-universalk9-mz.SPA.156-3.M6.bin"'
print(re.search(r'"flash:/(.*)"', output).group(1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM