From fafe3217883ef38e30c8b1fd0b1c175c71a9cb48 Mon Sep 17 00:00:00 2001 From: user901423 Date: Wed, 8 Mar 2017 02:09:18 -0800 Subject: [PATCH] Mac compatibility achieved --- ping.inc.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ping.inc.py b/ping.inc.py index 17517ce..e825443 100755 --- a/ping.inc.py +++ b/ping.inc.py @@ -67,7 +67,7 @@ class Pinger: pingout = Util.doPing(target) ssidout = Util.getSsid() - print json.dumps({'ssid': ssidout, 'loss': int(pingout)}) + print json.dumps({'ssid': ssidout, 'loss': float(pingout)}) def __init__(self): # Print welcome message @@ -92,8 +92,13 @@ class Util: out, error = ssid.communicate() m = re.search(r'yes:(.*)', out) result = m.group(1) - elif platform.system() == "Macintosh": - foo + elif platform.system() == "Darwin": + ssid = subprocess.Popen(["/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport","-I"], + stdout = subprocess.PIPE, + stderr = subprocess.PIPE + ) + out, error = ssid.communicate() + result = out return result @staticmethod @@ -112,8 +117,20 @@ class Util: pingout = -1 else: pingout = m.group(1) - elif platform.system() == "Macintosh": - foo + elif platform.system() == "Darwin": # Macintosh + ping = subprocess.Popen( + ["ping", "-c", "5", target], + stdout = subprocess.PIPE, + stderr = subprocess.PIPE + ) + out, error = ping.communicate() + # print out + m = re.search(r' ([0-9\.]+)% packet loss', out, re.MULTILINE) # (^rtt .*$)| + # print m.groups() + if error or m.group(1) == 100: + pingout = -1 + else: + pingout = m.group(1) return pingout #