-
Notifications
You must be signed in to change notification settings - Fork 1
/
iwd-scan.py
executable file
·83 lines (67 loc) · 2.86 KB
/
iwd-scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
#
# Usage:
# iwd-scan.py [ssid]
# DESCRIPTION:
# Outputs scanned SSIDs from of all wifi devices via dbus. Outputs in the following format:
# SSID/n
# Signal Strength dBm\n
# psk|wpa|open \n
# OPTIONS:
# ssid
# print just the SSID data, followed by a newline character
import sys
import dbus
import collections
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object("net.connman.iwd", "/"),
"org.freedesktop.DBus.ObjectManager")
objects = manager.GetManagedObjects()
Obj = collections.namedtuple('Obj', ['interfaces', 'children'])
tree = Obj({}, {})
for path in objects:
node = tree
elems = path.split('/')
for subpath in [ '/'.join(elems[:l + 1]) for l in range(1, len(elems)) ]:
if subpath not in node.children:
node.children[subpath] = Obj({}, {})
node = node.children[subpath]
node.interfaces.update(objects[path])
root = tree.children['/net'].children['/net/connman'].children['/net/connman/iwd']
for path, phy in root.children.items():
if 'net.connman.iwd.Adapter' not in phy.interfaces:
continue
properties = phy.interfaces['net.connman.iwd.Adapter']
for path2, device in phy.children.items():
if 'net.connman.iwd.Device' not in device.interfaces:
continue
if len(sys.argv) !=2 or (len(sys.argv) == 2 and sys.argv[1] != 'ssid'):
edevice = dbus.Interface(bus.get_object("net.connman.iwd", path2),
"net.connman.iwd.Station")
eprint("Scanning: [ %s ]" % path2)
try:
edevice.Scan()
except dbus.exceptions.DBusException as e:
eprint("Scan already in progress: %s" % e)
eprint("Defaulting to use existing scan")
for interface in device.interfaces:
name = interface.rsplit('.', 1)[-1]
if name not in ('Device', 'Station', 'AccessPoint', 'AdHoc'):
continue
properties = device.interfaces[interface]
if name != 'Station':
continue
eprint("Networks:")
station = dbus.Interface(bus.get_object("net.connman.iwd", path2),
'net.connman.iwd.Station')
for path3, rssi in station.GetOrderedNetworks():
properties2 = objects[path3]['net.connman.iwd.Network']
if len(sys.argv) !=2 or (len(sys.argv) == 2 and sys.argv[1] != 'ssid'):
print("%ls" % (properties2['Name'], ))
print("%i" % (rssi / 100,))
print("%s" % (properties2['Type'],))
print("%s" % ( properties2['Connected'], ))
else:
print("%ls" % (properties2['Name'],))