supplicant-assoc-unassoc.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Expects wpa_supplicant to be running as:
00004 # wpa_supplicant -i wlan1 -c /u/blaise/temp/net.conf -C /var/run/wpa_supplicant
00005 
00006 from __future__ import with_statement
00007 
00008 import subprocess
00009 import time
00010 import sys
00011 
00012 def call(cmd):
00013     print "\nCMD-->", "'"+("' '".join(cmd))+"'"
00014     with open('/dev/null', 'w') as dev_null:
00015         subprocess.call(cmd, stdout = dev_null)
00016 
00017 def wpa_cli(iface, cmd):
00018     call(['wpa_cli', '-p', '/var/run/wpa_supplicant', '-i', iface] + cmd)
00019 
00020 def disconnect(iface, network_id):
00021     if network_id is None:
00022         call(['iw', 'dev', iface, 'disconnect'])
00023     else:
00024         wpa_cli(iface, ['disconnect'])
00025 
00026 def connect(iface, essid, freq, bssid, network_id):
00027     if network_id is None:
00028         call(['iw', 'dev', iface, 'connect', essid, freq, bssid])
00029     else:
00030         #wpa_cli(iface, ['select_network', network_id])
00031         #wpa_cli(iface, ['bssid', network_id, bssid])
00032         wpa_cli(iface, ['reconnect'])
00033 
00034 def is_connected(iface):
00035     p = subprocess.Popen(['iw', 'dev', iface, 'link'], stdout = subprocess.PIPE)
00036     stdout, _ = p.communicate()
00037     if stdout.startswith("Not connected"):
00038         return False
00039     elif stdout.startswith("Connected to"):
00040         return True
00041     elif stdout.startswith("Authenticated"):
00042         return None
00043     else:
00044         raise Exception("Unexpected response:\n%s"%stdout)
00045 
00046 def wait_connect_state(iface, state, timeout = 10):
00047     end_time = time.time() + timeout
00048     while time.time() < end_time:
00049         if is_connected(iface) == state:
00050             return True
00051         time.sleep(0.1)
00052     return False
00053 
00054 def cycle_test(iface, essid, freq, bssid, network_id):
00055     cycles = 0
00056     successes = 0
00057     if network_id is None:
00058         call([ 'ifconfig', iface, 'up' ])
00059     while True:
00060         cycles += 1
00061         print "Unassociating...",
00062         sys.stdout.flush()
00063         disconnect(iface, network_id)
00064         if not wait_connect_state(iface, False):
00065             print "\nFailed to get interface in disconnected state. Bailing!"
00066             break;
00067         print "done.",
00068         sys.stdout.flush()
00069         time.sleep(0.5)
00070         
00071         print "Associating...",
00072         sys.stdout.flush()
00073         connect(iface, essid, freq, bssid, network_id)
00074         if not wait_connect_state(iface, True):
00075             print "FAIL.",
00076         else:
00077             print "done.",
00078             successes += 1
00079         print "Success %i/%i"%(successes, cycles)
00080         sys.stdout.flush()
00081         time.sleep(0.5)
00082 
00083 # Parameters are all strings:
00084 # interface, essid, frequency, bssid, wpa_supplicant network id. 
00085 cycle_test('wlan2', 'blaise-test', '5240', '00:25:9C:C4:D7:7E', '0')


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:21