test.py 1.9 KB

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