~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to wpa_supplicant/examples/wpas-test.py

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
import dbus
4
 
import sys, os
5
 
import time
6
 
 
7
 
WPAS_DBUS_SERVICE = "fi.epitest.hostap.WPASupplicant"
8
 
WPAS_DBUS_INTERFACE = "fi.epitest.hostap.WPASupplicant"
9
 
WPAS_DBUS_OPATH = "/fi/epitest/hostap/WPASupplicant"
10
 
 
11
 
WPAS_DBUS_INTERFACES_INTERFACE = "fi.epitest.hostap.WPASupplicant.Interface"
12
 
WPAS_DBUS_INTERFACES_OPATH = "/fi/epitest/hostap/WPASupplicant/Interfaces"
13
 
WPAS_DBUS_BSSID_INTERFACE = "fi.epitest.hostap.WPASupplicant.BSSID"
14
 
 
15
 
def 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
 
 
25
 
def 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
 
 
89
 
if __name__ == "__main__":
90
 
        main()
91