AttributeError: NoneType object has no attribute group in fping output

2024-06-16 08:27:09 发布

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

有时我会犯这个错误。这是一个简单的ping脚本,解析fping输出。当很多设备无法访问时-有时会出现以下错误:

massping.service - MassPing Service
   Loaded: loaded (/etc/systemd/system/massping.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2018-04-09 21:04:12 CEST; 22min ago
 Main PID: 936 (code=exited, status=1/FAILURE)

Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: ret = self.job_func()
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: File "/etc/massping/MassPingV3.py", line 115, in dowork
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: write2influx()
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: File "/etc/massping/MassPingV3.py", line 101, in write2influx
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: influxdata = createtabledata()
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: File "/etc/massping/MassPingV3.py", line 82, in createtabledata
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: iplist = getpingresults()
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: File "/etc/massping/MassPingV3.py", line 63, in getpingresults
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: ipaddress = m.group(1)
Apr 09 21:04:12 dcoxid001prvjay python3.6[936]: AttributeError: 'NoneType' object has no attribute 'group'

这是不应该的行为。我试图模拟这个场景,通过禁止ICMP通信来模拟不可访问性,但没有成功-脚本正常工作。你知道吗

我们应该得到这样的输出:

10.22.80.2   : 0.47 0.36 0.32
10.3.0.253   : - - -
10.3.0.254   : - - -
10.254.3.105 : - - -
10.254.3.106 : - - -
10.22.80.2   : 0.49 0.37 0.38
10.3.0.253   : - - -
10.3.0.254   : - - -
10.254.3.105 : - - -
10.254.3.106 : - - -
10.22.80.2   : 0.33 0.40 0.44

IP地址、ping结果或-(连字符)表示设备无法访问。你知道吗

我认为有可能我收到了一些未定义的输出,而脚本没有预料到这一点,并且在解析时出错。但我也不能模仿出现错误的行为。脚本函数如下:

def getpingresults():
        iplist = dict(load_devicefile())
        cmd = "/usr/sbin/fping -C 3 -A -q {}".format(" ".join(map(str, iplist.keys())))
        exitcode, out, results = get_fping_output(cmd)

        pingresults = []
        for aline in results.split("\n"):
                #print('Working on line: {0}'.format(aline))
                if aline:
                        m = re.match(r"(\S+)\s+:\s(\S+)\s(\S+)\s(\S+)", aline)
                        ipaddress = m.group(1)
                        ploss = False 
                        sum = 0
                        for i in m.group(2,3,4):
                             if i == '-':
                                ploss = True
                             else:
                                sum = sum + float(i)                            
                        if ploss == True:
                             iplist[ipaddress] += (float(9999),)
                        else:
                             sum = sum/3                             
                             #iplist[ipaddress] += (float(sum),)
                             iplist[ipaddress] += (str(sum)[:5],)

        #print(iplist)
        return iplist

有什么建议吗?谢谢

用试试这个方法行吗?你知道吗

def getpingresults():
    iplist = dict(load_devicefile())
    cmd = "/usr/sbin/fping -C 3 -A -q {}".format(" ".join(map(str, iplist.keys())))
    exitcode, out, results = get_fping_output(cmd)

    pingresults = []
    for aline in results.split("\n"):
            #print('Working on line: {0}'.format(aline))
            if aline:
                try:
                    m = re.match(r"(\S+)\s+:\s(\S+)\s(\S+)\s(\S+)", aline)
                    ipaddress = m.group(1)
                    ploss = False
                    sum = 0
                    for i in m.group(2,3,4):
                         if i == '-':
                            ploss = True
                         else:
                            sum = sum + float(i)
                    if ploss == True:
                         iplist[ipaddress] += (float(9999),)
                    else:
                         sum = sum/3
                         #iplist[ipaddress] += (float(sum),)
                         iplist[ipaddress] += (str(sum)[:5],)
                except AttributeError:
                    iplist[ipaddress] += (float(9999),)
    #print(iplist)
    return iplist

Tags: iniflinegroupetcfloatapripaddress
1条回答
网友
1楼 · 发布于 2024-06-16 08:27:09

显然没有匹配项,换句话说,fping的输出并不总是匹配这个regexp。您可以尝试将ipaddress的初始化放在try:块中进行研究,如果AttributeError出现,则记录aline以查看实际fping的输出是什么。你知道吗

相关问题 更多 >