Improving regex and output detail

This commit is contained in:
Will Bradley 2017-03-13 04:09:56 -07:00
parent 9f20431887
commit 155b25cb4a

View File

@ -64,10 +64,15 @@ import json
class Pinger: class Pinger:
def ping(self, target, log=None, widget=None, data=None): def ping(self, target, log=None, widget=None, data=None):
pingout = Util.doPing(target) pingResult = Util.doPing(target)
ssidout = Util.getSsid() ssidResult = Util.getSsid()
print json.dumps({'ssid': ssidout, 'loss': int(pingout)}) print json.dumps({
'target': target,
'ssid': ssidResult,
'loss': float(pingResult['loss']),
'rtt_avg': float(pingResult['rtt_avg']),
})
def __init__(self): def __init__(self):
# Print welcome message # Print welcome message
@ -104,17 +109,17 @@ class Util:
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE stderr = subprocess.PIPE
) )
out, error = ping.communicate() pingOut, pingError = ping.communicate()
#print out # Doing two regexes because Python doesn't like doing both in one
m = re.search(r' ([0-9]+)% packet loss', out, re.MULTILINE) # (^rtt .*$)| lossResult = re.search(r' ([0-9]+)% packet loss', pingOut, re.MULTILINE)
# print m.groups() rttAvgResult = re.search(r' = [\d\.]+/([\d\.]+)/[\d\.]+/[\d\.]+', pingOut, re.MULTILINE)
if error or m.group(1) == 100: if pingError:
pingout = -1 output = {'loss':-1, 'rtt_avg':-1};
else: else:
pingout = m.group(1) output = {'loss':lossResult.group(1), 'rtt_avg':rttAvgResult.group(1)};
elif platform.system() == "Macintosh": elif platform.system() == "Macintosh":
foo foo
return pingout return output
# #
# Runtime # Runtime