In this article, we are going to get a Wi-Fi password using a python script.
Introduction:
A simple python script that tells you the password of the Wi-Fi you’re connected with. We just need to install the python system. This program helps us to get the Wi-Fi password. This is just in 20 lines of code.
Outline:
What exactly is Wi-Fi?
A wireless or Wi-Fi network uses a radio frequency signal instead of wires to connect your devices – such as computers, printers, and smartphones – to the Internet and each other. The Wi-Fi signal can be picked up by any wireless-capable device such as a laptop or tablet within a certain distance in all directions.
What is a Wi-Fi password?
It’s a security standard for encrypting data over Wi-Fi that increases safety. This is the password to connect to your wireless network. It’s also called a Wi-Fi Security Key.
Project Prerequisites:
We need to install the subprocess module to run this code.
subprocess:
Subprocess in Python is a module used to run new codes and applications by creating new processes. . It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.
Code Implementation:
In this program we use
We need to import the sub-process module in this way.
import subprocess
Sub-process. Check output (): this is used to get the output of the calling program in python. . The args argument holds the commands that are to be passed as a string.
We need to decode the output so that we get strings back and not bytes.
The split () method splits a string into a list.
Decoding UTF-8 Strings in Python
To decode a string encoded in UTF-8 format, we can use the decode () method specified on strings. This method accepts two arguments, encoding and error. Encoding accepts the encoding of the string to be decoded, and error decides how to handle errors that arise during decoding.
data = (
subprocess.check_output([“netsh”, “wlan”, “show”, “profiles”])
.decode(“utf-8”)
.split(“\n”)
)
profiles = [i.split(“:”)[1][1:-1] for i in data if “All User Profile” in i]
for i in profiles:
results = (
subprocess
.check_output([“netsh”, “wlan”, “show”, “profile”, i, “key=clear”])
.decode(“utf-8”)
.split(“\n”)
)
results = [b.split(“:”)[1][1:-1] for b in results if “Key Content” in b]
data = (
subprocess.check_output(["netsh", "wlan", "show", "profiles"])
.decode("utf-8")
.split("\n")
)
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
for i in profiles:
results = (
subprocess
.check_output(["netsh", "wlan", "show", "profile", i, "key=clear"])
.decode("utf-8")
.split("\n")
)
results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause.
try:
print(“{:<30}| {:<}”.format(i, results[0]))
except IndexError:
print(“{:<30}| {:<}”.format(i, “”))
try:
print("{:<30}| {:<}".format(i, results[0]))
except IndexError:
print("{:<30}| {:<}".format(i, ""))
We have completed the coding part now let’s look at the complete source code.
Source code:
Here is the complete source code of the project.
import subprocess
data = (
subprocess.check_output(["netsh", "wlan", "show", "profiles"])
.decode("utf-8")
.split("\n")
)
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
for i in profiles:
results = (
subprocess
.check_output(["netsh", "wlan", "show", "profile", i, "key=clear"])
.decode("utf-8")
.split("\n")
)
results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
try:
print("{:<30}| {:<}".format(i, results[0]))
except IndexError:
print("{:<30}| {:<}".format(i, ""))
We have completed the coding part now it’s time to run our program.
Output:
Run the file from your code editor or Ide or u can also run it from the command line.
This is the password of the Wi-Fi that I am connected with.
Congratulations, you have learned how to get a wifi password using a simple python script.