wpas-dbus-new.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.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.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
  24. dbus_interface=dbus.PROPERTIES_IFACE)
  25. for path in ifaces:
  26. if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
  27. ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
  28. dbus_interface=dbus.PROPERTIES_IFACE)
  29. print ifname
  30. def propertiesChanged(properties):
  31. if properties.has_key("State"):
  32. print "PropertiesChanged: State: %s" % (properties["State"])
  33. def showBss(bss):
  34. net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
  35. net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
  36. # Convert the byte-array for SSID and BSSID to printable strings
  37. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
  38. dbus_interface=dbus.PROPERTIES_IFACE)
  39. bssid = ""
  40. for item in val:
  41. bssid = bssid + ":%02x" % item
  42. bssid = bssid[1:]
  43. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
  44. dbus_interface=dbus.PROPERTIES_IFACE)
  45. ssid = byte_array_to_string(val)
  46. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPA',
  47. dbus_interface=dbus.PROPERTIES_IFACE)
  48. wpa = "no"
  49. if len(val["KeyMgmt"]) > 0:
  50. wpa = "yes"
  51. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN',
  52. dbus_interface=dbus.PROPERTIES_IFACE)
  53. wpa2 = "no"
  54. if len(val["KeyMgmt"]) > 0:
  55. wpa2 = "yes"
  56. freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
  57. dbus_interface=dbus.PROPERTIES_IFACE)
  58. signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
  59. dbus_interface=dbus.PROPERTIES_IFACE)
  60. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
  61. dbus_interface=dbus.PROPERTIES_IFACE)
  62. if len(val) > 0:
  63. maxrate = val[0] / 1000000
  64. else:
  65. maxrate = 0
  66. print " %s :: ssid='%s' wpa=%s wpa2=%s signal=%d rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq)
  67. def scanDone(success):
  68. print "Scan done: success=%s" % success
  69. res = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'BSSs',
  70. dbus_interface=dbus.PROPERTIES_IFACE)
  71. print "Scanned wireless networks:"
  72. for opath in res:
  73. print opath
  74. showBss(opath)
  75. def bssAdded(bss, properties):
  76. print "BSS added: %s" % (bss)
  77. showBss(bss)
  78. def bssRemoved(bss):
  79. print "BSS removed: %s" % (bss)
  80. def main():
  81. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  82. global bus
  83. bus = dbus.SystemBus()
  84. wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
  85. if len(sys.argv) != 2:
  86. list_interfaces(wpas_obj)
  87. os._exit(1)
  88. wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
  89. bus.add_signal_receiver(scanDone,
  90. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  91. signal_name="ScanDone")
  92. bus.add_signal_receiver(bssAdded,
  93. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  94. signal_name="BSSAdded")
  95. bus.add_signal_receiver(bssRemoved,
  96. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  97. signal_name="BSSRemoved")
  98. bus.add_signal_receiver(propertiesChanged,
  99. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  100. signal_name="PropertiesChanged")
  101. ifname = sys.argv[1]
  102. # See if wpa_supplicant already knows about this interface
  103. path = None
  104. try:
  105. path = wpas.GetInterface(ifname)
  106. except dbus.DBusException, exc:
  107. if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
  108. raise exc
  109. try:
  110. path = wpas.CreateInterface({'Ifname': ifname, 'Driver': 'test'})
  111. time.sleep(1)
  112. except dbus.DBusException, exc:
  113. if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
  114. raise exc
  115. global if_obj
  116. if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
  117. global iface
  118. iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
  119. iface.Scan({'Type': 'active'})
  120. gobject.MainLoop().run()
  121. wpas.RemoveInterface(dbus.ObjectPath(path))
  122. if __name__ == "__main__":
  123. main()