2013-03-01 22:27:56 +01:00
|
|
|
#!/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
|
2016-03-04 10:20:32 +01:00
|
|
|
import sys
|
2013-03-01 22:27:56 +01:00
|
|
|
import time
|
|
|
|
import wpaspy
|
|
|
|
|
|
|
|
wpas_ctrl = '/var/run/wpa_supplicant'
|
|
|
|
|
2016-03-04 10:20:32 +01:00
|
|
|
def wpas_connect(host=None, port=9877):
|
2013-03-01 22:27:56 +01:00
|
|
|
ifaces = []
|
2016-03-04 10:20:32 +01:00
|
|
|
|
|
|
|
if host != None:
|
|
|
|
try:
|
|
|
|
wpas = wpaspy.Ctrl(host, port)
|
|
|
|
return wpas
|
|
|
|
except:
|
2019-01-24 08:45:42 +01:00
|
|
|
print("Could not connect to host: ", host)
|
2016-03-04 10:20:32 +01:00
|
|
|
return None
|
|
|
|
|
2013-03-01 22:27:56 +01:00
|
|
|
if os.path.isdir(wpas_ctrl):
|
|
|
|
try:
|
|
|
|
ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
|
2019-01-24 08:45:41 +01:00
|
|
|
except OSError as error:
|
2019-01-24 08:45:42 +01:00
|
|
|
print("Could not find wpa_supplicant: ", error)
|
2013-03-01 22:27:56 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
if len(ifaces) < 1:
|
2019-01-24 08:45:42 +01:00
|
|
|
print("No wpa_supplicant control interface found")
|
2013-03-01 22:27:56 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
for ctrl in ifaces:
|
|
|
|
try:
|
|
|
|
wpas = wpaspy.Ctrl(ctrl)
|
|
|
|
return wpas
|
2019-01-24 08:45:41 +01:00
|
|
|
except Exception as e:
|
2013-03-01 22:27:56 +01:00
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-03-04 10:20:32 +01:00
|
|
|
def main(host=None, port=9877):
|
2019-01-24 08:45:42 +01:00
|
|
|
print("Testing wpa_supplicant control interface connection")
|
2016-03-04 10:20:32 +01:00
|
|
|
wpas = wpas_connect(host, port)
|
2013-03-01 22:27:56 +01:00
|
|
|
if wpas is None:
|
|
|
|
return
|
2019-01-24 08:45:42 +01:00
|
|
|
print("Connected to wpa_supplicant")
|
|
|
|
print(wpas.request('PING'))
|
2013-03-01 22:27:56 +01:00
|
|
|
|
2016-03-04 10:20:32 +01:00
|
|
|
mon = wpas_connect(host, port)
|
2013-03-01 22:27:56 +01:00
|
|
|
if mon is None:
|
2019-01-24 08:45:42 +01:00
|
|
|
print("Could not open event monitor connection")
|
2013-03-01 22:27:56 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
mon.attach()
|
2019-01-24 08:45:42 +01:00
|
|
|
print("Scan")
|
|
|
|
print(wpas.request('SCAN'))
|
2013-03-01 22:27:56 +01:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
while count < 10:
|
|
|
|
count += 1
|
|
|
|
time.sleep(1)
|
|
|
|
while mon.pending():
|
|
|
|
ev = mon.recv()
|
2019-01-24 08:45:42 +01:00
|
|
|
print(ev)
|
2013-03-01 22:27:56 +01:00
|
|
|
if 'CTRL-EVENT-SCAN-RESULTS' in ev:
|
2019-01-24 08:45:42 +01:00
|
|
|
print('Scan completed')
|
|
|
|
print(wpas.request('SCAN_RESULTS'))
|
2013-03-01 22:27:56 +01:00
|
|
|
count = 10
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-03-04 10:20:32 +01:00
|
|
|
if len(sys.argv) > 2:
|
|
|
|
main(host=sys.argv[1], port=int(sys.argv[2]))
|
|
|
|
else:
|
|
|
|
main()
|