]>
Commit | Line | Data |
---|---|---|
20da5c89 JM |
1 | #!/usr/bin/python |
2 | # | |
3 | # Test script for wpaspy | |
4 | # Copyright (c) 2013, Jouni Malinen <j@w1.fi> | |
5 | # | |
6 | # This software may be distributed under the terms of the BSD license. | |
7 | # See README for more details. | |
8 | ||
9 | import os | |
a2c88a80 | 10 | import sys |
20da5c89 JM |
11 | import time |
12 | import wpaspy | |
13 | ||
14 | wpas_ctrl = '/var/run/wpa_supplicant' | |
15 | ||
a2c88a80 | 16 | def wpas_connect(host=None, port=9877): |
20da5c89 | 17 | ifaces = [] |
a2c88a80 JD |
18 | |
19 | if host != None: | |
20 | try: | |
21 | wpas = wpaspy.Ctrl(host, port) | |
22 | return wpas | |
23 | except: | |
89896c00 | 24 | print("Could not connect to host: ", host) |
a2c88a80 JD |
25 | return None |
26 | ||
20da5c89 JM |
27 | if os.path.isdir(wpas_ctrl): |
28 | try: | |
29 | ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)] | |
bab493b9 | 30 | except OSError as error: |
89896c00 | 31 | print("Could not find wpa_supplicant: ", error) |
20da5c89 JM |
32 | return None |
33 | ||
34 | if len(ifaces) < 1: | |
89896c00 | 35 | print("No wpa_supplicant control interface found") |
20da5c89 JM |
36 | return None |
37 | ||
38 | for ctrl in ifaces: | |
39 | try: | |
40 | wpas = wpaspy.Ctrl(ctrl) | |
41 | return wpas | |
bab493b9 | 42 | except Exception as e: |
20da5c89 JM |
43 | pass |
44 | return None | |
45 | ||
46 | ||
a2c88a80 | 47 | def main(host=None, port=9877): |
89896c00 | 48 | print("Testing wpa_supplicant control interface connection") |
a2c88a80 | 49 | wpas = wpas_connect(host, port) |
20da5c89 JM |
50 | if wpas is None: |
51 | return | |
89896c00 MH |
52 | print("Connected to wpa_supplicant") |
53 | print(wpas.request('PING')) | |
20da5c89 | 54 | |
a2c88a80 | 55 | mon = wpas_connect(host, port) |
20da5c89 | 56 | if mon is None: |
89896c00 | 57 | print("Could not open event monitor connection") |
20da5c89 JM |
58 | return |
59 | ||
60 | mon.attach() | |
89896c00 MH |
61 | print("Scan") |
62 | print(wpas.request('SCAN')) | |
20da5c89 JM |
63 | |
64 | count = 0 | |
65 | while count < 10: | |
66 | count += 1 | |
67 | time.sleep(1) | |
68 | while mon.pending(): | |
69 | ev = mon.recv() | |
89896c00 | 70 | print(ev) |
20da5c89 | 71 | if 'CTRL-EVENT-SCAN-RESULTS' in ev: |
89896c00 MH |
72 | print('Scan completed') |
73 | print(wpas.request('SCAN_RESULTS')) | |
20da5c89 JM |
74 | count = 10 |
75 | pass | |
76 | ||
77 | ||
78 | if __name__ == "__main__": | |
a2c88a80 JD |
79 | if len(sys.argv) > 2: |
80 | main(host=sys.argv[1], port=int(sys.argv[2])) | |
81 | else: | |
82 | main() |