aboutsummaryrefslogtreecommitdiffstats
path: root/wpaspy/test.py
blob: 9141a8d404f1d1d677d4e91b1591789941323b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
#
# Test script for wpaspy
# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

import os
import sys
import time
import wpaspy

wpas_ctrl = '/var/run/wpa_supplicant'

def wpas_connect(host=None, port=9877):
    ifaces = []

    if host != None:
        try:
            wpas = wpaspy.Ctrl(host, port)
            return wpas
        except:
            print "Could not connect to host: ", host
            return None

    if os.path.isdir(wpas_ctrl):
        try:
            ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
        except OSError, error:
            print "Could not find wpa_supplicant: ", error
            return None

    if len(ifaces) < 1:
        print "No wpa_supplicant control interface found"
        return None

    for ctrl in ifaces:
        try:
            wpas = wpaspy.Ctrl(ctrl)
            return wpas
        except Exception, e:
            pass
    return None


def main(host=None, port=9877):
    print "Testing wpa_supplicant control interface connection"
    wpas = wpas_connect(host, port)
    if wpas is None:
        return
    print "Connected to wpa_supplicant"
    print wpas.request('PING')

    mon = wpas_connect(host, port)
    if mon is None:
        print "Could not open event monitor connection"
        return

    mon.attach()
    print "Scan"
    print wpas.request('SCAN')

    count = 0
    while count < 10:
        count += 1
        time.sleep(1)
        while mon.pending():
            ev = mon.recv()
            print ev
            if 'CTRL-EVENT-SCAN-RESULTS' in ev:
                print 'Scan completed'
                print wpas.request('SCAN_RESULTS')
                count = 10
                pass


if __name__ == "__main__":
    if len(sys.argv) > 2:
        main(host=sys.argv[1], port=int(sys.argv[2]))
    else:
        main()