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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#!/usr/bin/python3
# Copyright (c) 2017-2019 David Steele <dsteele@gmail.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# License-Filename: LICENSE
import logging
import subprocess
import re
from multiprocessing import Process, Queue
log = logging.getLogger("comitup")
# NetworkManager is doing a poor job of maintaining the AP scan list when
# in AP mode. Use the 'iw' command to get the AP list.
def docmd(cmd):
cmd = "timeout 5 " + cmd
try:
out = subprocess.check_output(cmd.split()).decode()
except subprocess.CalledProcessError:
out = ""
return out
def devlist():
"""Get a list of supported devices from 'iw'"""
log.debug("Getting device list from 'iw'")
out = docmd("iw dev")
devs = [x.split()[1] for x in out.split('\n') if "Interface" in x]
return devs
def blk2dict(blk):
"""Convert a 'iw dev scan' block into a tagged dict"""
lines = [x.strip().split(':') for x in blk.split('\n') if ':' in x]
tups = [(x[0].strip(), x[1].strip()) for x in lines if len(x) > 1]
return dict(tups)
def dbm2pct(dbm):
pct = (dbm + 100.0) * 2
pct = max(0, pct)
pct = min(100, pct)
return str(pct)
def devaps(dev, dump=""):
"""Get a list of Access Points (as dicts) for a device"""
if not dump:
log.debug("Getting AP list from 'iw' dev %s" % dev)
dump = docmd('iw dev %s scan' % dev)
aps = []
for blk in re.split('\nBSS ', dump[4:]):
try:
ap = blk2dict(blk)
ap['power'] = dbm2pct(float(ap['signal'].split()[0]))
if ap['SSID']:
aps.append(ap)
except KeyError:
pass
return aps
def apgen(dev, q, dump=""):
for ap in devaps(dev, dump):
pt = {}
pt['ssid'] = ap['SSID']
pt['strength'] = ap['power']
if 'WPA' in ap or 'RSN' in ap:
pt['security'] = 'encrypted'
else:
pt['security'] = 'unencrypted'
q.put(pt)
q.put("DONE")
def dedup_aplist(aplist):
apdict = {x['ssid']: x for x in aplist}
return [apdict[x] for x in apdict]
def candidates(device=None):
"""Return a list of reachable Access Point SSIDs, sorted by power"""
if device:
dev_list = [device]
else:
dev_list = devlist()
jobs = []
q = Queue()
for dev in dev_list:
p = Process(target=apgen, args=(dev, q))
p.start()
jobs.append(p)
clist = []
donecount = 0
while donecount < len(jobs):
pt = q.get()
if pt == "DONE":
donecount += 1
else:
clist.append(pt)
for p in jobs:
p.join()
clist = dedup_aplist(clist)
clist = sorted(clist, key=lambda x: -float(x['strength']))
return clist
def ap_conn_count():
count = 0
for dev in devlist():
log.debug("Getting iw station dump")
out = docmd('iw dev %s station dump' % dev)
count += len([x for x in out.split('\n') if "Station" in x])
return count
if __name__ == '__main__':
print(candidates())
print(ap_conn_count())
import sys
for ap in devaps(sys.argv[1], sys.stdin.read()):
print(ap["SSID"], ap["RSN"], ap["power"], "RSN" in ap)
|