Last active
August 29, 2015 14:18
-
-
Save stefanor/c443d766a1e6abafaa56 to your computer and use it in GitHub Desktop.
PyCon Wifi
This file contains 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 | |
import argparse | |
import logging | |
import platform | |
import subprocess | |
import time | |
import requests | |
from requests.exceptions import ConnectionError, ReadTimeout | |
def get_ip_linux(interface): | |
output = subprocess.check_output(( | |
'ip', '-f', 'inet', 'addr', 'show', interface)) | |
for line in output.splitlines(): | |
parts = line.split() | |
if parts[0] == 'inet': | |
ip = parts[1].split('/')[0] | |
return ip | |
return None | |
def get_ip_darwin(interface): | |
subprocess.check_output(('ifconfig', 'en0')) | |
output = subprocess.check_output(('ifconfig', interface)) | |
for line in output.splitlines(): | |
parts = line.split() | |
if parts[0] == 'inet': | |
ip = parts[1].split('/')[0] | |
return ip | |
return None | |
def login(): | |
while True: | |
logging.info('Logging in...') | |
try: | |
requests.post('http://webauth.congresmtl.com/login.html', | |
data={'buttonClicked': 4}, | |
timeout=10) | |
logging.info('Logged in') | |
return | |
except (ConnectionError, ReadTimeout) as e: | |
logging.debug('Retrying, %s', e) | |
pass | |
def main(): | |
p = argparse.ArgumentParser() | |
p.add_argument('--iface', default='wlan0', help='WiFi interface') | |
p.add_argument('--verbose', '-v', action='store_true', | |
help='Increase verbosity') | |
args = p.parse_args() | |
logging.basicConfig( | |
format='%(asctime)-15s %(message)s', | |
level=logging.DEBUG if args.verbose else logging.INFO, | |
) | |
if platform.system().startswith('Linux'): | |
get_ip = get_ip_linux | |
elif platform.system() == 'Darwin': | |
get_ip = get_ip_darwin | |
else: | |
raise NotImplemented("Unsupported platform") | |
ip = None | |
try: | |
while True: | |
old_ip, ip = ip, get_ip(args.iface) | |
if ip and old_ip != ip: | |
login() | |
time.sleep(1) | |
except KeyboardInterrupt: | |
pass | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For osx: