[go: up one dir, main page]

File: iwscan.py

package info (click to toggle)
comitup 1.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,252 kB
  • sloc: python: 3,093; javascript: 1,261; sh: 95; makefile: 34
file content (171 lines) | stat: -rw-r--r-- 4,304 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/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 re
import struct
import subprocess
from multiprocessing import Process, Queue
from typing import Dict, List, Optional

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: str) -> str:
    cmd = "timeout 5 " + cmd

    try:
        out = subprocess.check_output(
            cmd.split(), encoding="utf-8", stderr=subprocess.PIPE
        )
    except subprocess.CalledProcessError:
        out = ""

    return out


def devlist() -> List[str]:
    """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: str) -> Dict[str, str]:
    """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: float) -> str:
    pct = (dbm + 100.0) * 2
    pct = max(0, pct)
    pct = min(100, pct)
    return str(pct)


def decode_x(s: str) -> str:
    """Take a unicode string of the form "b\xc3\xbct" and fix it"""
    s = re.sub("\x00", "", s, count=0, flags=re.I)
    s = re.sub("'", "\\'", s, count=0, flags=re.I)
    s = eval("'{}'".format(s))
    outlist = []
    for cp in s:
        num = ord(cp)
        if num < 255:
            outlist.append(struct.pack("B", num))
        elif num < 65535:
            outlist.append(struct.pack(">H", num))
        else:
            b = (num & 0xFF0000) >> 16
            H = num & 0xFFFF
            outlist.append(struct.pack(">bH", b, H))
    blist = b"".join(outlist)
    return blist.decode("utf-8")


def devaps(dev: str, dump: str = "") -> List[Dict[str, str]]:
    """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"] and ap["SSID"].count(r"\x00") == 0:
                ap["SSID"] = decode_x(ap["SSID"])
                aps.append(ap)
        except KeyError:
            pass

    return aps


def apgen(dev: str, q: Queue, dump: str = "") -> None:
    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: List[Dict[str, str]]) -> List[Dict[str, str]]:
    apdict = {x["ssid"]: x for x in aplist}

    return [apdict[x] for x in apdict]


def candidates(device: Optional[str] = None) -> List[Dict[str, str]]:
    """Return a list of reachable Access Point SSIDs, sorted by power"""

    if device:
        dev_list = [device]
    else:
        dev_list = devlist()

    jobs = []
    q: Queue = 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() -> int:
    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)