18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#!/usr/bin/python 28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtimport dbus 48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtimport sys, os 58d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtimport time 68d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 78d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtWPAS_DBUS_SERVICE = "fi.epitest.hostap.WPASupplicant" 88d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtWPAS_DBUS_INTERFACE = "fi.epitest.hostap.WPASupplicant" 98d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtWPAS_DBUS_OPATH = "/fi/epitest/hostap/WPASupplicant" 108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 118d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtWPAS_DBUS_INTERFACES_INTERFACE = "fi.epitest.hostap.WPASupplicant.Interface" 128d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtWPAS_DBUS_INTERFACES_OPATH = "/fi/epitest/hostap/WPASupplicant/Interfaces" 138d520ff1dc2da35cdca849e982051b86468016d8Dmitry ShmidtWPAS_DBUS_BSSID_INTERFACE = "fi.epitest.hostap.WPASupplicant.BSSID" 148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtdef byte_array_to_string(s): 168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt import urllib 178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt r = "" 188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for c in s: 198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if c >= 32 and c < 127: 208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt r += "%c" % c 218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt else: 228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt r += urllib.quote(chr(c)) 238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return r 248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtdef main(): 268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if len(sys.argv) != 2: 278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt print "Usage: wpas-test.py <interface>" 288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt os._exit(1) 298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ifname = sys.argv[1] 318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bus = dbus.SystemBus() 338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH) 348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE) 358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt # See if wpa_supplicant already knows about this interface 378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt path = None 388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt try: 398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt path = wpas.getInterface(ifname) 408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt except dbus.dbus_bindings.DBusException, exc: 418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if str(exc) != "wpa_supplicant knows nothing about this interface.": 428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt raise exc 438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt try: 448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt path = wpas.addInterface(ifname, {'driver': dbus.Variant('wext')}) 458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt except dbus.dbus_bindings.DBusException, exc: 468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if str(exc) != "wpa_supplicant already controls this interface.": 478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt raise exc 488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if_obj = bus.get_object(WPAS_DBUS_SERVICE, path) 508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE) 518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt iface.scan() 528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt # Should really wait for the "scanResults" signal instead of sleeping 538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt time.sleep(5) 548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt res = iface.scanResults() 558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt print "Scanned wireless networks:" 578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for opath in res: 588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt net_obj = bus.get_object(WPAS_DBUS_SERVICE, opath) 598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt net = dbus.Interface(net_obj, WPAS_DBUS_BSSID_INTERFACE) 608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt props = net.properties() 618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt # Convert the byte-array for SSID and BSSID to printable strings 638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bssid = "" 648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt for item in props["bssid"]: 658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bssid = bssid + ":%02x" % item 668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bssid = bssid[1:] 678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ssid = byte_array_to_string(props["ssid"]) 688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpa = "no" 698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if props.has_key("wpaie"): 708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpa = "yes" 718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpa2 = "no" 728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if props.has_key("rsnie"): 738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpa2 = "yes" 748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt freq = 0 758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if props.has_key("frequency"): 768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt freq = props["frequency"] 778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt caps = props["capabilities"] 788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt qual = props["quality"] 798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt level = props["level"] 808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt noise = props["noise"] 818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt maxrate = props["maxrate"] / 1000000 828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt print " %s :: ssid='%s' wpa=%s wpa2=%s quality=%d%% rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, qual, maxrate, freq) 848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt wpas.removeInterface(dbus.ObjectPath(path)) 868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt # Should fail here with unknown interface error 878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt iface.scan() 888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtif __name__ == "__main__": 908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt main() 918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 92