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