Skip to content

Commit 8b0c1bb

Browse files
committed
add listing wifi networks tutorial
1 parent d330440 commit 8b0c1bb

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4444
- [How to Make a Phone Number Tracker in Python](https://thepythoncode.com/article/phone-number-tracker-in-python). ([code](ethical-hacking/phone-number-tracker))
4545
- [How to Make a Login Password Guesser in Python](https://thepythoncode.com/article/make-a-login-password-guesser-in-python). ([code](ethical-hacking/login-password-guesser))
4646
- [How to Build a Password Manager in Python](https://thepythoncode.com/article/build-a-password-manager-in-python). ([code](ethical-hacking/password-manager))
47+
- [How to List Wi-Fi Networks in Python](https://thepythoncode.com/article/list-nearby-wifi-networks-with-python). ([code](ethical-hacking/listing-wifi-networks))
4748

4849
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
4950
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to List Wi-Fi Networks in Python](https://thepythoncode.com/article/list-nearby-wifi-networks-with-python)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import subprocess, platform, re
2+
from colorama import init, Fore
3+
4+
init()
5+
6+
7+
def list_open_networks():
8+
# Get the name of the operating system.
9+
os_name = platform.system()
10+
11+
# Check if the OS is Windows.
12+
if os_name == "Windows":
13+
# Command to list Wi-Fi networks on Windows.
14+
list_networks_command = 'netsh wlan show networks'
15+
try:
16+
# Execute the command and capture the output.
17+
output = subprocess.check_output(list_networks_command, shell=True, text=True)
18+
networks = []
19+
20+
# Parse the output to find open Wi-Fi networks.
21+
for line in output.splitlines():
22+
if "SSID" in line:
23+
# Extract the SSID (Wi-Fi network name).
24+
ssid = line.split(":")[1].strip()
25+
elif "Authentication" in line and "Open" in line:
26+
# Check if the Wi-Fi network has open authentication.
27+
networks.append(ssid)
28+
29+
# Check if any open networks were found.
30+
if len(networks) > 0:
31+
# Print a message for open networks with colored output.
32+
print(f'{Fore.LIGHTMAGENTA_EX}[+] Open Wifi networks in range: \n')
33+
for each_network in networks:
34+
print(f"{Fore.GREEN}[+] {each_network}")
35+
else:
36+
# Print a message if no open networks were found.
37+
print(f"{Fore.RED}[-] No open wifi networks in range")
38+
39+
except subprocess.CalledProcessError as e:
40+
# Handle any errors that occur during the execution of the command.
41+
print(f"{Fore.RED}Error: {e}")
42+
# Return an empty list to indicate that no networks were found.
43+
return []
44+
45+
elif os_name == "Linux":
46+
try:
47+
# Run nmcli to list available Wi-Fi networks.
48+
result = subprocess.run(["nmcli", "--fields", "SECURITY,SSID", "device", "wifi", "list"],
49+
stdout=subprocess.PIPE,
50+
text=True, check=True)
51+
52+
# Access the captured stdout.
53+
output = result.stdout.strip()
54+
55+
# Define a regex pattern to capture SSID and Security.
56+
pattern = re.compile(r'^(?P<security>[^\s]+)\s+(?P<ssid>.+)$', re.MULTILINE)
57+
58+
# Find all matches in the output.
59+
matches = pattern.finditer(output)
60+
61+
# Skip the first match, which is the header.
62+
next(matches, None)
63+
print(f"{Fore.LIGHTMAGENTA_EX}[+] Open Wifi networks in range: \n")
64+
# Loop through all matches (results)
65+
for match in matches:
66+
security = match.group('security')
67+
ssid = match.group('ssid')
68+
full_match = f"{Fore.GREEN}[+] SSID: {ssid} -------> Security: {security}"
69+
# Check if the indicator of an open network in our Full match (result).
70+
if "Security: --" in full_match:
71+
print(f"{Fore.GREEN}[+] {ssid}")
72+
else:
73+
print(f"{Fore.RED}[-] No open Wifi networks in range.")
74+
75+
except subprocess.CalledProcessError as e:
76+
print(f"Error running nmcli: {e}")
77+
except Exception as e:
78+
print(f"An error occurred: {e}")
79+
80+
else:
81+
print(f"{Fore.RED}Unsupported operating system.")
82+
return []
83+
84+
85+
# Call the function.
86+
list_open_networks()
87+
88+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import subprocess, platform
2+
3+
4+
# Get the name of the operating system.
5+
os_name = platform.system()
6+
7+
# Check if the OS is Windows.
8+
if os_name == "Windows":
9+
# Command to list Wi-Fi networks on Windows using netsh.
10+
list_networks_command = 'netsh wlan show networks'
11+
12+
# Execute the command and capture the result.
13+
output = subprocess.check_output(list_networks_command, shell=True, text=True)
14+
15+
# Print the output, all networks in range.
16+
print(output)
17+
18+
# Check if the OS is Linux.
19+
elif os_name == "Linux":
20+
# Command to list Wi-Fi networks on Linux using nmcli.
21+
list_networks_command = "nmcli device wifi list"
22+
23+
# Execute the command and capture the output.
24+
output = subprocess.check_output(list_networks_command, shell=True, text=True)
25+
26+
# Print the output, all networks in range.
27+
print(output)
28+
29+
# Handle unsupported operating systems.
30+
else:
31+
# Print a message indicating that the OS is unsupported (Not Linux or Windows).
32+
print("Unsupported OS")

0 commit comments

Comments
 (0)