-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi_scanner.py
225 lines (176 loc) · 6.81 KB
/
wifi_scanner.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import re
import time
import platform
import argparse
import subprocess
def scan_wifi():
system_platform = platform.system().lower()
if system_platform == "windows":
return scan_wifi_windows()
elif system_platform == "linux":
return scan_wifi_linux()
elif system_platform == "darwin":
return scan_wifi_mac()
else:
print("Unsupported platform for Wi-Fi scanning.")
return []
def scan_wifi_windows():
try:
result = subprocess.run(
["netsh", "wlan", "show", "networks", "mode=Bssid"],
capture_output=True, text=True, shell=True
)
if result.returncode != 0:
print("Error: Unable to scan networks. Make sure Wi-Fi is enabled.")
return []
output = result.stdout
if not output.strip():
print("No Wi-Fi networks found.")
return []
return parse_networks_windows(output)
except Exception as e:
print(f"Error: {e}")
return []
def scan_wifi_linux():
try:
result = subprocess.run(
["nmcli", "-t", "-f", "SSID,SIGNAL,SECURITY", "device", "wifi"],
capture_output=True, text=True
)
if result.returncode != 0:
print("Error: Unable to scan networks. Make sure Wi-Fi is enabled.")
return []
output = result.stdout
if not output.strip():
print("No Wi-Fi networks found.")
return []
return parse_networks_linux(output)
except Exception as e:
print(f"Error: {e}")
return []
def scan_wifi_mac():
try:
result = subprocess.run(
["/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport", "-s"],
capture_output=True, text=True
)
if result.returncode != 0:
print("Error: Unable to scan networks. Make sure Wi-Fi is enabled.")
return []
output = result.stdout
if not output.strip():
print("No Wi-Fi networks found.")
return []
return parse_networks_mac(output)
except Exception as e:
print(f"Error: {e}")
return []
def parse_networks_windows(output):
ssid_pattern = re.compile(r"SSID \d+ : (.+)")
signal_pattern = re.compile(r"Signal\s+:\s+(\d+)%")
auth_pattern = re.compile(r"Authentication\s+:\s+(.+)")
encryption_pattern = re.compile(r"Encryption\s+:\s+(.+)")
channel_pattern = re.compile(r"Channel\s+:\s+(\d+)")
band_pattern = re.compile(r"Band\s+:\s+(.+)")
lines = output.splitlines()
networks = []
current_network = {}
for line in lines:
ssid_match = ssid_pattern.search(line)
if ssid_match:
if current_network:
networks.append(current_network)
current_network = {'SSID': ssid_match.group(1)}
if signal_match := signal_pattern.search(line):
current_network['Signal'] = signal_match.group(1)
if auth_match := auth_pattern.search(line):
current_network['Authentication'] = auth_match.group(1)
if encryption_match := encryption_pattern.search(line):
current_network['Encryption'] = encryption_match.group(1)
if channel_match := channel_pattern.search(line):
current_network['Channel'] = channel_match.group(1)
if band_match := band_pattern.search(line):
current_network['Band'] = band_match.group(1)
if current_network:
networks.append(current_network)
return networks
def parse_networks_linux(output):
networks = []
lines = output.strip().splitlines()
for line in lines:
ssid, signal, security = line.split(":")
networks.append({
"SSID": ssid.strip(),
"Signal": signal.strip(),
"Authentication": security.strip(),
"Encryption": security.strip()
})
return networks
def parse_networks_mac(output):
networks = []
lines = output.strip().splitlines()
for line in lines[1:]:
parts = line.split()
ssid = " ".join(parts[:-3])
signal = parts[-3]
networks.append({
"SSID": ssid.strip(),
"Signal": signal.strip(),
"Authentication": "Unknown",
"Encryption": "Unknown"
})
return networks
def parse_networks(output):
ssid_pattern = re.compile(r"SSID \d+ : (.+)")
signal_pattern = re.compile(r"Signal\s+:\s+(\d+)%")
auth_pattern = re.compile(r"Authentication\s+:\s+(.+)")
encryption_pattern = re.compile(r"Encryption\s+:\s+(.+)")
channel_pattern = re.compile(r"Channel\s+:\s+(\d+)")
band_pattern = re.compile(r"Band\s+:\s+(.+)")
lines = output.splitlines()
networks = []
current_network = {}
for line in lines:
ssid_match = ssid_pattern.search(line)
if ssid_match:
if current_network:
networks.append(current_network)
current_network = {'SSID': ssid_match.group(1)}
if signal_match := signal_pattern.search(line):
current_network['Signal'] = signal_match.group(1)
if auth_match := auth_pattern.search(line):
current_network['Authentication'] = auth_match.group(1)
if encryption_match := encryption_pattern.search(line):
current_network['Encryption'] = encryption_match.group(1)
if channel_match := channel_pattern.search(line):
current_network['Channel'] = channel_match.group(1)
if band_match := band_pattern.search(line):
current_network['Band'] = band_match.group(1)
if current_network:
networks.append(current_network)
return networks
def parse_arguments():
parser = argparse.ArgumentParser(description="Wi-Fi Network Scanner Tool")
parser.add_argument('-t', '--timeout', type=int, default=10, help='Timeout duration between scans in seconds')
parser.add_argument('-r', '--retries', type=int, default=5, help='Number of retries for scanning')
return parser.parse_args()
def main():
args = parse_arguments()
for retry in range(args.retries):
print(f"Scan attempt {retry + 1}/{args.retries}")
networks = scan_wifi()
if networks:
print("Available Wi-Fi Networks:")
for network in networks:
print(f"\nSSID: {network['SSID']}")
print(f" Signal Strength: {network['Signal']}%")
print(f" Authentication: {network['Authentication']}")
print(f" Encryption: {network['Encryption']}")
print(f" Channel: {network.get('Channel', 'None')}")
print(f" Band: {network.get('Band', 'None')}")
else:
print("No networks found.")
time.sleep(args.timeout)
print("Scanning completed!")
if __name__ == "__main__":
main()