1#!/usr/bin/python 2 3import dbus 4import sys, os 5import time 6import gobject 7from dbus.mainloop.glib import DBusGMainLoop 8 9WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1" 10WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1" 11WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1" 12 13WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface" 14WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces" 15WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.BSS" 16WPAS_DBUS_NETWORK_INTERFACE = "fi.w1.wpa_supplicant1.Network" 17 18def byte_array_to_string(s): 19 import urllib 20 r = "" 21 for c in s: 22 if c >= 32 and c < 127: 23 r += "%c" % c 24 else: 25 r += urllib.quote(chr(c)) 26 return r 27 28def list_interfaces(wpas_obj): 29 ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces', 30 dbus_interface=dbus.PROPERTIES_IFACE) 31 for path in ifaces: 32 if_obj = bus.get_object(WPAS_DBUS_SERVICE, path) 33 ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname', 34 dbus_interface=dbus.PROPERTIES_IFACE) 35 print ifname 36 37def interfaceAdded(interface, properties): 38 print "InterfaceAdded(%s): Ifname=%s" % (interface, properties['Ifname']) 39 40def interfaceRemoved(interface): 41 print "InterfaceRemoved(%s)" % (interface) 42 43def propertiesChanged(properties): 44 for i in properties: 45 print "PropertiesChanged: %s=%s" % (i, properties[i]) 46 47def showBss(bss): 48 net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss) 49 net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE) 50 51 # Convert the byte-array for SSID and BSSID to printable strings 52 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID', 53 dbus_interface=dbus.PROPERTIES_IFACE) 54 bssid = "" 55 for item in val: 56 bssid = bssid + ":%02x" % item 57 bssid = bssid[1:] 58 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID', 59 dbus_interface=dbus.PROPERTIES_IFACE) 60 ssid = byte_array_to_string(val) 61 62 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPA', 63 dbus_interface=dbus.PROPERTIES_IFACE) 64 wpa = "no" 65 if val != None: 66 wpa = "yes" 67 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN', 68 dbus_interface=dbus.PROPERTIES_IFACE) 69 wpa2 = "no" 70 if val != None: 71 wpa2 = "yes" 72 freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency', 73 dbus_interface=dbus.PROPERTIES_IFACE) 74 signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal', 75 dbus_interface=dbus.PROPERTIES_IFACE) 76 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates', 77 dbus_interface=dbus.PROPERTIES_IFACE) 78 if len(val) > 0: 79 maxrate = val[0] / 1000000 80 else: 81 maxrate = 0 82 83 print " %s :: ssid='%s' wpa=%s wpa2=%s signal=%d rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq) 84 85def scanDone(success): 86 gobject.MainLoop().quit() 87 print "Scan done: success=%s" % success 88 89def scanDone2(success, path=None): 90 print "Scan done: success=%s [path=%s]" % (success, path) 91 92def bssAdded(bss, properties): 93 print "BSS added: %s" % (bss) 94 showBss(bss) 95 96def bssRemoved(bss): 97 print "BSS removed: %s" % (bss) 98 99def blobAdded(blob): 100 print "BlobAdded(%s)" % (blob) 101 102def blobRemoved(blob): 103 print "BlobRemoved(%s)" % (blob) 104 105def networkAdded(network, properties): 106 print "NetworkAdded(%s)" % (network) 107 108def networkRemoved(network): 109 print "NetworkRemoved(%s)" % (network) 110 111def networkSelected(network): 112 print "NetworkSelected(%s)" % (network) 113 114def propertiesChangedInterface(properties): 115 for i in properties: 116 print "PropertiesChanged(interface): %s=%s" % (i, properties[i]) 117 118def propertiesChangedBss(properties): 119 for i in properties: 120 print "PropertiesChanged(BSS): %s=%s" % (i, properties[i]) 121 122def propertiesChangedNetwork(properties): 123 for i in properties: 124 print "PropertiesChanged(Network): %s=%s" % (i, properties[i]) 125 126def main(): 127 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 128 global bus 129 bus = dbus.SystemBus() 130 wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH) 131 132 if len(sys.argv) != 2: 133 list_interfaces(wpas_obj) 134 os._exit(1) 135 136 wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE) 137 bus.add_signal_receiver(interfaceAdded, 138 dbus_interface=WPAS_DBUS_INTERFACE, 139 signal_name="InterfaceAdded") 140 bus.add_signal_receiver(interfaceRemoved, 141 dbus_interface=WPAS_DBUS_INTERFACE, 142 signal_name="InterfaceRemoved") 143 bus.add_signal_receiver(propertiesChanged, 144 dbus_interface=WPAS_DBUS_INTERFACE, 145 signal_name="PropertiesChanged") 146 147 ifname = sys.argv[1] 148 path = wpas.GetInterface(ifname) 149 if_obj = bus.get_object(WPAS_DBUS_SERVICE, path) 150 iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE) 151 iface.connect_to_signal("ScanDone", scanDone2, 152 path_keyword='path') 153 154 bus.add_signal_receiver(scanDone, 155 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 156 signal_name="ScanDone", 157 path=path) 158 bus.add_signal_receiver(bssAdded, 159 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 160 signal_name="BSSAdded", 161 path=path) 162 bus.add_signal_receiver(bssRemoved, 163 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 164 signal_name="BSSRemoved", 165 path=path) 166 bus.add_signal_receiver(blobAdded, 167 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 168 signal_name="BlobAdded", 169 path=path) 170 bus.add_signal_receiver(blobRemoved, 171 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 172 signal_name="BlobRemoved", 173 path=path) 174 bus.add_signal_receiver(networkAdded, 175 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 176 signal_name="NetworkAdded", 177 path=path) 178 bus.add_signal_receiver(networkRemoved, 179 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 180 signal_name="NetworkRemoved", 181 path=path) 182 bus.add_signal_receiver(networkSelected, 183 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 184 signal_name="NetworkSelected", 185 path=path) 186 bus.add_signal_receiver(propertiesChangedInterface, 187 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE, 188 signal_name="PropertiesChanged", 189 path=path) 190 191 bus.add_signal_receiver(propertiesChangedBss, 192 dbus_interface=WPAS_DBUS_BSS_INTERFACE, 193 signal_name="PropertiesChanged") 194 195 bus.add_signal_receiver(propertiesChangedNetwork, 196 dbus_interface=WPAS_DBUS_NETWORK_INTERFACE, 197 signal_name="PropertiesChanged") 198 199 gobject.MainLoop().run() 200 201if __name__ == "__main__": 202 main() 203 204