00001
00002
00003 import dbus
00004 import sys, os
00005 import time
00006
00007 WPAS_DBUS_SERVICE = "fi.epitest.hostap.WPASupplicant"
00008 WPAS_DBUS_INTERFACE = "fi.epitest.hostap.WPASupplicant"
00009 WPAS_DBUS_OPATH = "/fi/epitest/hostap/WPASupplicant"
00010
00011 WPAS_DBUS_INTERFACES_INTERFACE = "fi.epitest.hostap.WPASupplicant.Interface"
00012 WPAS_DBUS_INTERFACES_OPATH = "/fi/epitest/hostap/WPASupplicant/Interfaces"
00013 WPAS_DBUS_BSSID_INTERFACE = "fi.epitest.hostap.WPASupplicant.BSSID"
00014
00015 def byte_array_to_string(s):
00016 import urllib
00017 r = ""
00018 for c in s:
00019 if c >= 32 and c < 127:
00020 r += "%c" % c
00021 else:
00022 r += urllib.quote(chr(c))
00023 return r
00024
00025 def main():
00026 if len(sys.argv) != 2:
00027 print "Usage: wpas-test.py <interface>"
00028 os._exit(1)
00029
00030 ifname = sys.argv[1]
00031
00032 bus = dbus.SystemBus()
00033 wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
00034 wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
00035
00036
00037 path = None
00038 try:
00039 path = wpas.getInterface(ifname)
00040 except dbus.dbus_bindings.DBusException, exc:
00041 if str(exc) != "wpa_supplicant knows nothing about this interface.":
00042 raise exc
00043 try:
00044 path = wpas.addInterface(ifname, {'driver': dbus.Variant('wext')})
00045 except dbus.dbus_bindings.DBusException, exc:
00046 if str(exc) != "wpa_supplicant already controls this interface.":
00047 raise exc
00048
00049 if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
00050 iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
00051 iface.scan()
00052
00053 time.sleep(5)
00054 res = iface.scanResults()
00055
00056 print "Scanned wireless networks:"
00057 for opath in res:
00058 net_obj = bus.get_object(WPAS_DBUS_SERVICE, opath)
00059 net = dbus.Interface(net_obj, WPAS_DBUS_BSSID_INTERFACE)
00060 props = net.properties()
00061
00062
00063 bssid = ""
00064 for item in props["bssid"]:
00065 bssid = bssid + ":%02x" % item
00066 bssid = bssid[1:]
00067 ssid = byte_array_to_string(props["ssid"])
00068 wpa = "no"
00069 if props.has_key("wpaie"):
00070 wpa = "yes"
00071 wpa2 = "no"
00072 if props.has_key("rsnie"):
00073 wpa2 = "yes"
00074 freq = 0
00075 if props.has_key("frequency"):
00076 freq = props["frequency"]
00077 caps = props["capabilities"]
00078 qual = props["quality"]
00079 level = props["level"]
00080 noise = props["noise"]
00081 maxrate = props["maxrate"] / 1000000
00082
00083 print " %s :: ssid='%s' wpa=%s wpa2=%s quality=%d%% rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, qual, maxrate, freq)
00084
00085 wpas.removeInterface(dbus.ObjectPath(path))
00086
00087 iface.scan()
00088
00089 if __name__ == "__main__":
00090 main()
00091