wpas-test.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python
  2. import dbus
  3. import sys, os
  4. import time
  5. WPAS_DBUS_SERVICE = "fi.epitest.hostap.WPASupplicant"
  6. WPAS_DBUS_INTERFACE = "fi.epitest.hostap.WPASupplicant"
  7. WPAS_DBUS_OPATH = "/fi/epitest/hostap/WPASupplicant"
  8. WPAS_DBUS_INTERFACES_INTERFACE = "fi.epitest.hostap.WPASupplicant.Interface"
  9. WPAS_DBUS_INTERFACES_OPATH = "/fi/epitest/hostap/WPASupplicant/Interfaces"
  10. WPAS_DBUS_BSSID_INTERFACE = "fi.epitest.hostap.WPASupplicant.BSSID"
  11. def byte_array_to_string(s):
  12. import urllib
  13. r = ""
  14. for c in s:
  15. if c >= 32 and c < 127:
  16. r += "%c" % c
  17. else:
  18. r += urllib.quote(chr(c))
  19. return r
  20. def main():
  21. if len(sys.argv) != 2:
  22. print "Usage: wpas-test.py <interface>"
  23. os._exit(1)
  24. ifname = sys.argv[1]
  25. bus = dbus.SystemBus()
  26. wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
  27. wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
  28. # See if wpa_supplicant already knows about this interface
  29. path = None
  30. try:
  31. path = wpas.getInterface(ifname)
  32. except dbus.dbus_bindings.DBusException, exc:
  33. if str(exc) != "wpa_supplicant knows nothing about this interface.":
  34. raise exc
  35. try:
  36. path = wpas.addInterface(ifname, {'driver': dbus.Variant('wext')})
  37. except dbus.dbus_bindings.DBusException, exc:
  38. if str(exc) != "wpa_supplicant already controls this interface.":
  39. raise exc
  40. if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
  41. iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
  42. iface.scan()
  43. # Should really wait for the "scanResults" signal instead of sleeping
  44. time.sleep(5)
  45. res = iface.scanResults()
  46. print "Scanned wireless networks:"
  47. for opath in res:
  48. net_obj = bus.get_object(WPAS_DBUS_SERVICE, opath)
  49. net = dbus.Interface(net_obj, WPAS_DBUS_BSSID_INTERFACE)
  50. props = net.properties()
  51. # Convert the byte-array for SSID and BSSID to printable strings
  52. bssid = ""
  53. for item in props["bssid"]:
  54. bssid = bssid + ":%02x" % item
  55. bssid = bssid[1:]
  56. ssid = byte_array_to_string(props["ssid"])
  57. wpa = "no"
  58. if props.has_key("wpaie"):
  59. wpa = "yes"
  60. wpa2 = "no"
  61. if props.has_key("rsnie"):
  62. wpa2 = "yes"
  63. freq = 0
  64. if props.has_key("frequency"):
  65. freq = props["frequency"]
  66. caps = props["capabilities"]
  67. qual = props["quality"]
  68. level = props["level"]
  69. noise = props["noise"]
  70. maxrate = props["maxrate"] / 1000000
  71. print " %s :: ssid='%s' wpa=%s wpa2=%s quality=%d%% rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, qual, maxrate, freq)
  72. wpas.removeInterface(dbus.ObjectPath(path))
  73. # Should fail here with unknown interface error
  74. iface.scan()
  75. if __name__ == "__main__":
  76. main()