-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathwifi_scanner.py
More file actions
99 lines (98 loc) · 3.1 KB
/
wifi_scanner.py
File metadata and controls
99 lines (98 loc) · 3.1 KB
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
#!/usr/bin/env python3
"""Simple Wi-Fi scanner
Provides a small CLI that scan nearby Wi-Fi networks using
nmcli, iw, or iwlist and parsers for their outputs.
"""
from subprocess import PIPE, Popen
import shutil
def run(cmd):
try:
p = Popen(cmd, stdout=PIPE, stderr=PIPE, text=True)
out,err = p.communicate(timeout=8)
return out
except Exception:
return ""
def parse_nmcli(out):
rows = []
for line in out.splitlines()[1:]:
parts = [p.strip() for p in line.split()]
if not parts: continue
# nmcli aligns columns; fallback simple split: last cols are SIGNAL CHAN SECURITY
ssid = line[:40].strip()
rest = line[40:].split()
if len(rest) >= 3:
bssid = rest[0]
signal = rest[1]
else:
bssid = ''
signal = rest[0] if rest else ''
rows.append((ssid,bssid,signal))
return rows
def parse_iw(out):
rows = []
ssid = bssid = signal = None
for l in out.splitlines():
l = l.strip()
if l.startswith('BSS '):
if ssid:
rows.append((ssid,bssid,signal))
bssid = l.split()[1]
ssid = signal = None
elif l.startswith('SSID:'):
ssid = l.split(':',1)[1].strip()
elif 'signal:' in l:
signal = l.split('signal:')[1].split()[0]
if ssid:
rows.append((ssid,bssid,signal))
return rows
def parse_iwlist(out):
rows=[]
ssid=bssid=signal=None
for l in out.splitlines():
l=l.strip()
if l.startswith('Cell '):
if ssid: rows.append((ssid,bssid,signal))
parts=l.split()
bssid=parts[4] if len(parts)>=5 else ''
ssid=signal=None
elif 'ESSID:' in l:
ssid=l.split('ESSID:')[1].strip('"')
elif 'Signal level=' in l:
signal=l.split('Signal level=')[1].split()[0]
if ssid: rows.append((ssid,bssid,signal))
return rows
def pretty_print(rows):
if not rows:
print('No networks found or scanning tools not available.')
return
print(f"{'SSID':40} {'BSSID':20} SIGNAL")
for s,b,sg in rows:
print(f"{s[:40]:40} {b:20} {sg}")
def main():
if shutil.which('nmcli'):
out = run(['nmcli','-f','SSID,BSSID,SIGNAL,CHAN,SECURITY','device','wifi','list'])
rows = parse_nmcli(out)
pretty_print(rows)
return
if shutil.which('iw'):
# find iface
out = run(['iw','dev'])
iface = None
for l in out.splitlines():
l=l.strip()
if l.startswith('Interface '):
iface = l.split()[1]; break
if iface:
out = run(['sudo','iw','dev',iface,'scan'])
rows = parse_iw(out)
pretty_print(rows)
return
if shutil.which('iwlist'):
iface = 'wlan0'
out = run(['sudo','iwlist',iface,'scanning'])
rows = parse_iwlist(out)
pretty_print(rows)
return
print('No scanning tools (nmcli/iw/iwlist) found. Please run on a Linux host with Wi‑Fi tools installed.')
if __name__=='__main__':
main()