Skip to content

Commit 21020c6

Browse files
committed
airport: sudo is required
airport: allow single spaces in SSID
1 parent 181e12c commit 21020c6

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[![ci](https://github.com/scivision/mozilla-location-wifi/actions/workflows/ci.yml/badge.svg)](https://github.com/scivision/mozilla-location-wifi/actions/workflows/ci.yml)
44
[![PyPi Download stats](http://pepy.tech/badge/mozloc)](http://pepy.tech/project/mozloc)
55

6-
Uses command line access to WiFi information in a short, simple Mozilla Location Services with Wifi from Python.
6+
Uses command line access to WiFi information via
7+
[Mozilla Location Services API](https://ichnaea.readthedocs.io/en/latest/api/geolocate.html?highlight=macaddress#wifi-access-point-fields)
8+
from Python.
79
The command line programs used to access WiFi information include:
810

911
* Linux: `nmcli` [NetworkManager](https://developer.gnome.org/NetworkManager/stable/nmcli.html)
@@ -12,7 +14,6 @@ The command line programs used to access WiFi information include:
1214

1315
Note that a similar service with better accuracy is available from
1416
[Google](https://developers.google.com/maps/documentation/geolocation/intro).
15-
Let us know if you're interested.
1617

1718
## Install
1819

@@ -38,7 +39,7 @@ python -m mozloc
3839
Shows `time` `lat` `lng` `accuracy` `N BSSIDs heard`.
3940
In urban areas, accuracy of less than 100 meters is possible.
4041

41-
### dump raw signals
42+
dump raw signals, without using API:
4243

4344
```sh
4445
python -m mozloc.signal
@@ -49,7 +50,7 @@ python -m mozloc.signal
4950
On Windows, NetSH is used.
5051
You may need to disconnect from WiFi (leave WiFi enabled) to make your WiFi chipset scan and be able to get location.
5152

52-
### convert to KML
53+
## convert to KML
5354

5455
Display logged data in Google Earth or other KML viewer after converting from CSV to KML:
5556

src/mozloc/airport.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import subprocess
77
import re
88

9-
from .cmd import get_airport
9+
from .cmd import get_airport, running_as_root
1010

1111

1212
def cli_config_check() -> bool:
@@ -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.0)
35+
ret = subprocess.check_output([get_airport(), "-s"], text=True, timeout=30)
3636
except subprocess.CalledProcessError as err:
3737
logging.error(f"consider slowing scan cadence. {err}")
3838

@@ -41,17 +41,31 @@ def get_signal() -> str:
4141

4242
def parse_signal(raw: str) -> list[dict[str, T.Any]]:
4343

44-
pat = re.compile(r"\s*([0-9a-zA-Z\-\.]+)\s+([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s+(-\d{2,3})")
44+
isroot = running_as_root()
45+
if not isroot:
46+
raise RuntimeError("airport requires running as sudo to get BSSID")
47+
48+
psudo = r"\s*([0-9a-zA-Z\s\-\.]+)\s+([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s+(-\d{2,3})"
49+
# BSSID only present if sudo
50+
# puser = r"\s*([0-9a-zA-Z\s\-\.]+)\s+(-\d{2,3})"
51+
# non-sudo has no BSSID
52+
53+
p = psudo
54+
i = 2
55+
56+
pat = re.compile(p)
4557
dat: list[dict[str, str]] = []
4658

4759
for line in raw.split("\n"):
4860
mat = pat.match(line)
4961
if mat:
62+
# Hidden SSID optout implicitly excluded by regex
5063
ssid = mat.group(1)
5164
# optout
5265
if ssid.endswith("_nomap"):
5366
continue
54-
55-
dat.append({"ssid": ssid, "macAddress": mat.group(2), "signalStrength": mat.group(3)})
67+
dat.append(
68+
{"ssid": ssid, "macAddress": mat.group(i), "signalStrength": mat.group(i + 1)}
69+
)
5670

5771
return dat

src/mozloc/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ 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-
logging.debug(dat)
49+
print(dat)
5050

5151
loc = get_loc_mozilla(dat, mozilla_url)
5252
if loc is None:

src/mozloc/cmd.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from __future__ import annotations
22

3+
import os
34
import functools
45
import shutil
56

67

8+
def running_as_root() -> bool:
9+
return os.getuid() == 0
10+
11+
712
@functools.cache
813
def get_exe(name: str, path: str | None = None) -> str:
914
if not (exe := shutil.which(name, path=path)):

0 commit comments

Comments
 (0)