Skip to content

Instantly share code, notes, and snippets.

@mapk0y
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapk0y/b2479eb4027a83c85c3c to your computer and use it in GitHub Desktop.
Save mapk0y/b2479eb4027a83c85c3c to your computer and use it in GitHub Desktop.
Get WIFI Access Point status for MacOSX
#!/usr/bin/python
# vi: set ts=4 sw=4 sts=0 et:
import sys,time,subprocess
from datetime import datetime
AIRPORT='/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'
AIRPORT_OPT=('-I')
cmdline=(AIRPORT,AIRPORT_OPT)
subproc_args = {'shell': False,
'stdin': subprocess.PIPE,
'stdout': subprocess.PIPE,
'stderr': subprocess.STDOUT,
'cwd': '/',
'close_fds': True,
}
while True:
try:
p = subprocess.Popen(cmdline, **subproc_args)
except OSError:
print("Failed to execute command: %s" % cmdline[0])
sys.exit(1)
(stdout, stdin) = (p.stdout, p.stdin)
data = {}
for line in stdout:
d = line.strip().split(':', 1)
data[d[0]] = d[1].strip()
try:
rssi = int(data["agrCtlRSSI"])
rssi_state = "bad"
if rssi > -60:
rssi_state = "good"
elif rssi > -75:
rssi_state = "normal"
print("[%s] %s(%s, %d/%dMbps, %d ch) - RSSI: %d(%s)" %
(datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
data["SSID"],
data["BSSID"],
int(data["lastTxRate"]),
int(data["maxRate"]),
int(data["channel"]),
rssi, rssi_state,
))
except KeyError:
print("[%s] Not Connected" % datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment