有时我会收到此错误。这是一个简单的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
答案 0 :(得分:0)
显然没有匹配,换句话说,fping的输出并不总是匹配此正则表达式。您可以尝试通过将try:
的初始化放在AttributeError
块中来调查它,如果aline
出现,请记录let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
this.events.publish('connectionMade', user, Date.now());
});
以查看实际的fping是什么&#39}。输出。