2009-12-26 17:41:01 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import dbus
|
|
|
|
import sys, os
|
|
|
|
import time
|
|
|
|
import gobject
|
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
|
|
|
|
WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
|
|
|
|
WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1"
|
|
|
|
WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1"
|
|
|
|
|
|
|
|
WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface"
|
|
|
|
WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces"
|
2010-01-09 10:40:15 +01:00
|
|
|
WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.BSS"
|
2009-12-26 17:41:01 +01:00
|
|
|
|
|
|
|
def byte_array_to_string(s):
|
|
|
|
import urllib
|
|
|
|
r = ""
|
|
|
|
for c in s:
|
|
|
|
if c >= 32 and c < 127:
|
|
|
|
r += "%c" % c
|
|
|
|
else:
|
|
|
|
r += urllib.quote(chr(c))
|
|
|
|
return r
|
|
|
|
|
|
|
|
def list_interfaces(wpas_obj):
|
2010-01-01 12:16:33 +01:00
|
|
|
ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
|
|
|
for path in ifaces:
|
|
|
|
if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
|
|
|
|
ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
|
|
|
print ifname
|
2009-12-26 17:41:01 +01:00
|
|
|
|
2010-01-04 15:34:06 +01:00
|
|
|
def propertiesChanged(properties):
|
|
|
|
if properties.has_key("State"):
|
|
|
|
print "PropertiesChanged: State: %s" % (properties["State"])
|
2009-12-26 17:41:01 +01:00
|
|
|
|
2009-12-27 20:42:02 +01:00
|
|
|
def showBss(bss):
|
|
|
|
net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
|
|
|
|
net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
|
|
|
|
|
|
|
|
# Convert the byte-array for SSID and BSSID to printable strings
|
2010-01-01 12:10:09 +01:00
|
|
|
val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
2009-12-27 20:42:02 +01:00
|
|
|
bssid = ""
|
2010-01-01 12:10:09 +01:00
|
|
|
for item in val:
|
2009-12-27 20:42:02 +01:00
|
|
|
bssid = bssid + ":%02x" % item
|
|
|
|
bssid = bssid[1:]
|
2010-01-01 12:10:09 +01:00
|
|
|
val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
|
|
|
ssid = byte_array_to_string(val)
|
2009-12-27 20:42:02 +01:00
|
|
|
|
2010-01-01 12:10:09 +01:00
|
|
|
val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPAIE',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
2009-12-27 20:42:02 +01:00
|
|
|
wpa = "no"
|
2010-01-01 12:10:09 +01:00
|
|
|
if val != None:
|
2009-12-27 20:42:02 +01:00
|
|
|
wpa = "yes"
|
2010-01-01 12:10:09 +01:00
|
|
|
val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSNIE',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
2009-12-27 20:42:02 +01:00
|
|
|
wpa2 = "no"
|
2010-01-01 12:10:09 +01:00
|
|
|
if val != None:
|
2009-12-27 20:42:02 +01:00
|
|
|
wpa2 = "yes"
|
2010-01-01 12:10:09 +01:00
|
|
|
freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
|
|
|
signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
2010-01-04 15:52:30 +01:00
|
|
|
val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
|
2010-01-01 12:10:09 +01:00
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
2010-01-04 15:52:30 +01:00
|
|
|
if len(val) > 0:
|
|
|
|
maxrate = val[0] / 1000000
|
|
|
|
else:
|
|
|
|
maxrate = 0
|
2009-12-27 20:42:02 +01:00
|
|
|
|
2010-01-01 12:10:09 +01:00
|
|
|
print " %s :: ssid='%s' wpa=%s wpa2=%s signal=%d rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq)
|
2009-12-27 20:42:02 +01:00
|
|
|
|
2009-12-26 17:41:01 +01:00
|
|
|
def scanDone(success):
|
|
|
|
print "Scan done: success=%s" % success
|
|
|
|
|
|
|
|
res = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'BSSs',
|
|
|
|
dbus_interface=dbus.PROPERTIES_IFACE)
|
|
|
|
|
|
|
|
print "Scanned wireless networks:"
|
|
|
|
for opath in res:
|
|
|
|
print opath
|
2009-12-27 20:42:02 +01:00
|
|
|
showBss(opath)
|
|
|
|
|
2010-01-01 12:10:09 +01:00
|
|
|
def bssAdded(bss, properties):
|
2009-12-27 20:42:02 +01:00
|
|
|
print "BSS added: %s" % (bss)
|
|
|
|
showBss(bss)
|
|
|
|
|
|
|
|
def bssRemoved(bss):
|
|
|
|
print "BSS removed: %s" % (bss)
|
2009-12-26 17:41:01 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
global bus
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
|
2010-01-01 12:16:33 +01:00
|
|
|
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
list_interfaces(wpas_obj)
|
|
|
|
os._exit(1)
|
|
|
|
|
2009-12-26 17:41:01 +01:00
|
|
|
wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
|
|
|
|
bus.add_signal_receiver(scanDone,
|
|
|
|
dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
|
|
|
|
signal_name="ScanDone")
|
2009-12-27 20:42:02 +01:00
|
|
|
bus.add_signal_receiver(bssAdded,
|
|
|
|
dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
|
|
|
|
signal_name="BSSAdded")
|
|
|
|
bus.add_signal_receiver(bssRemoved,
|
|
|
|
dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
|
|
|
|
signal_name="BSSRemoved")
|
2010-01-04 15:34:06 +01:00
|
|
|
bus.add_signal_receiver(propertiesChanged,
|
2009-12-26 17:41:01 +01:00
|
|
|
dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
|
2010-01-04 15:34:06 +01:00
|
|
|
signal_name="PropertiesChanged")
|
2009-12-26 17:41:01 +01:00
|
|
|
|
|
|
|
ifname = sys.argv[1]
|
|
|
|
|
|
|
|
# See if wpa_supplicant already knows about this interface
|
|
|
|
path = None
|
|
|
|
try:
|
|
|
|
path = wpas.GetInterface(ifname)
|
|
|
|
except dbus.DBusException, exc:
|
|
|
|
if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
|
|
|
|
raise exc
|
|
|
|
try:
|
|
|
|
path = wpas.CreateInterface({'Ifname': ifname, 'Driver': 'test'})
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
except dbus.DBusException, exc:
|
|
|
|
if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
global if_obj
|
|
|
|
if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
|
|
|
|
global iface
|
|
|
|
iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
|
|
|
|
iface.Scan({'Type': 'active'})
|
|
|
|
|
|
|
|
gobject.MainLoop().run()
|
|
|
|
|
|
|
|
wpas.RemoveInterface(dbus.ObjectPath(path))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|