wpas-dbus-new.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/python
  2. import dbus
  3. import sys, os
  4. import time
  5. import gobject
  6. from dbus.mainloop.glib import DBusGMainLoop
  7. WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
  8. WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1"
  9. WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1"
  10. WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface"
  11. WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces"
  12. WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.Interface.BSS"
  13. def byte_array_to_string(s):
  14. import urllib
  15. r = ""
  16. for c in s:
  17. if c >= 32 and c < 127:
  18. r += "%c" % c
  19. else:
  20. r += urllib.quote(chr(c))
  21. return r
  22. def list_interfaces(wpas_obj):
  23. ifaces = wpas_obj.Interfaces
  24. for i in ifaces:
  25. print "%s" (i)
  26. def stateChanged(newState, oldState):
  27. print "StateChanged(%s -> %s)" % (oldState, newState)
  28. def scanDone(success):
  29. gobject.MainLoop().quit()
  30. print "Scan done: success=%s" % success
  31. res = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'BSSs',
  32. dbus_interface=dbus.PROPERTIES_IFACE)
  33. print "Scanned wireless networks:"
  34. for opath in res:
  35. print opath
  36. net_obj = bus.get_object(WPAS_DBUS_SERVICE, opath)
  37. net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
  38. props = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Properties',
  39. dbus_interface=dbus.PROPERTIES_IFACE)
  40. #print props
  41. # Convert the byte-array for SSID and BSSID to printable strings
  42. bssid = ""
  43. for item in props['BSSID']:
  44. bssid = bssid + ":%02x" % item
  45. bssid = bssid[1:]
  46. ssid = byte_array_to_string(props["SSID"])
  47. wpa = "no"
  48. if props.has_key("WPAIE"):
  49. wpa = "yes"
  50. wpa2 = "no"
  51. if props.has_key("RSNIE"):
  52. wpa2 = "yes"
  53. freq = 0
  54. if props.has_key("Frequency"):
  55. freq = props["Frequency"]
  56. caps = props["Capabilities"]
  57. qual = props["Quality"]
  58. level = props["Level"]
  59. noise = props["Noise"]
  60. maxrate = props["MaxRate"] / 1000000
  61. print " %s :: ssid='%s' wpa=%s wpa2=%s quality=%d%% rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, qual, maxrate, freq)
  62. def main():
  63. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  64. global bus
  65. bus = dbus.SystemBus()
  66. wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
  67. wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
  68. bus.add_signal_receiver(scanDone,
  69. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  70. signal_name="ScanDone")
  71. bus.add_signal_receiver(stateChanged,
  72. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  73. signal_name="StateChanged")
  74. if len(sys.argv) != 2:
  75. list_interfaces(wpas_obj)
  76. os._exit(1)
  77. ifname = sys.argv[1]
  78. # See if wpa_supplicant already knows about this interface
  79. path = None
  80. try:
  81. path = wpas.GetInterface(ifname)
  82. except dbus.DBusException, exc:
  83. if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
  84. raise exc
  85. try:
  86. path = wpas.CreateInterface({'Ifname': ifname, 'Driver': 'test'})
  87. time.sleep(1)
  88. except dbus.DBusException, exc:
  89. if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
  90. raise exc
  91. global if_obj
  92. if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
  93. global iface
  94. iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
  95. iface.Scan({'Type': 'active'})
  96. gobject.MainLoop().run()
  97. wpas.RemoveInterface(dbus.ObjectPath(path))
  98. if __name__ == "__main__":
  99. main()