Skip to content

Commit cee0ea4

Browse files
committed
airport: allow less accurate non-sudo
1 parent 21020c6 commit cee0ea4

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ dump raw signals, without using API:
4545
python -m mozloc.signal
4646
```
4747

48+
### macOS
49+
50+
On macOS, much more accurate results come by running as root by using sudo.
51+
This is because "airport" only emits BSSID if running with sudo.
52+
53+
Possible future implementation could use
54+
[CoreWLAN](https://developer.apple.com/documentation/corewlan/).
55+
4856
### Windows
4957

5058
On Windows, NetSH is used.

src/mozloc/airport.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def cli_config_check() -> bool:
3232
def get_signal() -> str:
3333

3434
try:
35-
ret = subprocess.check_output([get_airport(), "-s"], text=True, timeout=30)
35+
ret = subprocess.check_output([get_airport(), "--scan"], text=True, timeout=30)
3636
except subprocess.CalledProcessError as err:
3737
logging.error(f"consider slowing scan cadence. {err}")
3838

@@ -42,30 +42,29 @@ def get_signal() -> str:
4242
def parse_signal(raw: str) -> list[dict[str, T.Any]]:
4343

4444
isroot = running_as_root()
45-
if not isroot:
46-
raise RuntimeError("airport requires running as sudo to get BSSID")
4745

4846
psudo = r"\s*([0-9a-zA-Z\s\-\.]+)\s+([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s+(-\d{2,3})"
4947
# BSSID only present if sudo
50-
# puser = r"\s*([0-9a-zA-Z\s\-\.]+)\s+(-\d{2,3})"
48+
puser = r"\s*([0-9a-zA-Z\s\-\.]+)\s+(-\d{2,3})"
5149
# non-sudo has no BSSID
5250

53-
p = psudo
54-
i = 2
55-
51+
p = psudo if isroot else puser
52+
isig = 3 if isroot else 2
53+
ibssid = 2
5654
pat = re.compile(p)
5755
dat: list[dict[str, str]] = []
5856

5957
for line in raw.split("\n"):
6058
mat = pat.match(line)
6159
if mat:
6260
# Hidden SSID optout implicitly excluded by regex
63-
ssid = mat.group(1)
61+
ssid = mat.group(1).strip()
6462
# optout
6563
if ssid.endswith("_nomap"):
6664
continue
67-
dat.append(
68-
{"ssid": ssid, "macAddress": mat.group(i), "signalStrength": mat.group(i + 1)}
69-
)
65+
d = {"ssid": ssid, "signalStrength": mat.group(isig)}
66+
if isroot:
67+
d["macAddress"] = mat.group(ibssid)
68+
dat.append(d)
7069

7170
return dat

src/mozloc/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def log_wifi_loc(cadence_sec: float, mozilla_url: str, logfile: Path | None = No
4646
logging.warning(f"cannot locate since at least 2 BSSIDs required\n{dat}")
4747
sleep(cadence_sec)
4848
continue
49-
print(dat)
5049

5150
loc = get_loc_mozilla(dat, mozilla_url)
5251
if loc is None:

0 commit comments

Comments
 (0)