Created
June 11, 2014 14:57
-
-
Save mstaflex/b85da386ddc451184d89 to your computer and use it in GitHub Desktop.
Monitor the connection state to an Access Point on a Mac OS computer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Tool to output the state of connection to an AP on a Mac OS computer. | |
Usage: | |
mac_connected_ap.py [options] [-q | -v] INTERFACE... | |
Arguments: | |
INTERFACE Interface to use [default: "en1"] | |
Other options: | |
-h, --help show this help message and exit | |
-q, --quiet print less text | |
-v, --verbose print more text | |
--version show version and exit | |
""" | |
__author__ = "Jasper Buesch" | |
__copyright__ = "Copyright (c) 2014, Technische Universität Berlin" | |
__version__ = "0.1.0" | |
__email__ = "[email protected]" | |
# coding: utf-8 | |
import subprocess | |
import time | |
import logging | |
call = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I en1" | |
def get_if_state(cmd_call): | |
return subprocess.check_output(cmd_call, shell=True) | |
def main(args): | |
cmd_call = call % (args["INTERFACE"]) | |
log = logging.getLogger("main") | |
while True: | |
output = get_if_state(cmd_call) | |
if output.strip().startswith("AirPort: Off"): | |
log.fatal("Airport reports that WIFI is switched off!") | |
while output.strip().startswith("AirPort: Off"): | |
output = get_if_state(cmd_call) | |
time.sleep(0.5) | |
continue | |
output_lines = output.split("\n") | |
bssid = None | |
agrCtlRSSI = None | |
for line in output_lines: | |
striped_line = line.strip() | |
if striped_line.startswith("agrCtlRSSI: "): | |
agrCtlRSSI = striped_line.split("agrCtlRSSI: ")[1] | |
if striped_line.startswith("BSSID: "): | |
bssid = striped_line.split("BSSID: ")[1] | |
break | |
if bssid == "0:0:0:0:0:0": | |
log.info("Not connected") | |
else: | |
log.info("Connected to AP %s with RSSI %s" % (bssid, agrCtlRSSI)) | |
time.sleep(1) | |
if __name__ == "__main__": | |
try: | |
from docopt import docopt | |
except: | |
print(""" | |
Please install docopt using: | |
pip install docopt==0.6.1 | |
For more refer to: | |
https://github.com/docopt/docopt | |
""") | |
raise | |
args = docopt(__doc__, version=__version__) | |
log_level = logging.INFO # default | |
if args['--verbose']: | |
log_level = logging.DEBUG | |
elif args['--quiet']: | |
log_level = logging.ERROR | |
logging.basicConfig(level=log_level, | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment