Updated to hostap_2_6
[mech_eap.git] / libeap / wpaspy / test.py
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
10 import sys
11 import time
12 import wpaspy
13
14 wpas_ctrl = '/var/run/wpa_supplicant'
15
16 def wpas_connect(host=None, port=9877):
17     ifaces = []
18
19     if host != None:
20         try:
21             wpas = wpaspy.Ctrl(host, port)
22             return wpas
23         except:
24             print "Could not connect to host: ", host
25             return None
26
27     if os.path.isdir(wpas_ctrl):
28         try:
29             ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
30         except OSError, error:
31             print "Could not find wpa_supplicant: ", error
32             return None
33
34     if len(ifaces) < 1:
35         print "No wpa_supplicant control interface found"
36         return None
37
38     for ctrl in ifaces:
39         try:
40             wpas = wpaspy.Ctrl(ctrl)
41             return wpas
42         except Exception, e:
43             pass
44     return None
45
46
47 def main(host=None, port=9877):
48     print "Testing wpa_supplicant control interface connection"
49     wpas = wpas_connect(host, port)
50     if wpas is None:
51         return
52     print "Connected to wpa_supplicant"
53     print wpas.request('PING')
54
55     mon = wpas_connect(host, port)
56     if mon is None:
57         print "Could not open event monitor connection"
58         return
59
60     mon.attach()
61     print "Scan"
62     print wpas.request('SCAN')
63
64     count = 0
65     while count < 10:
66         count += 1
67         time.sleep(1)
68         while mon.pending():
69             ev = mon.recv()
70             print ev
71             if 'CTRL-EVENT-SCAN-RESULTS' in ev:
72                 print 'Scan completed'
73                 print wpas.request('SCAN_RESULTS')
74                 count = 10
75                 pass
76
77
78 if __name__ == "__main__":
79     if len(sys.argv) > 2:
80         main(host=sys.argv[1], port=int(sys.argv[2]))
81     else:
82         main()