Skip to main content

Get Wifi Passwords using Python

In our previous Python tutorial, we have developed Snake Game using Python. In this tutorial, we will explain how to get saved WiFi Passwords with Python.

While connecting with multiple devices, we sometimes forget passwords. So here we will exaplain how easily you can get all saved wifi passwords with connection details.

With Python, you can easily get the saved Wifi passwords details using subprocess module. The module allow to check the conencted wifi passwords by running netsh commands inside our Python program.

so let’s start coding to get Wifi Password:

Command to check connected wifi passwords

There are two command needs to run to get the details of saved Wifi passwords.

netsh wlan show profile
netsh wlan show profile PROFILE-NAME key=clear

The first command returns the profiles of the connected wifi and second command show the connected wifi password.

Implement To Get Wifi Password

We will import subprocess module to run cmd commands in our python program.

we will use function check_output () from this module to run the both command and get profile data.

import subprocess

data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')

we will convert the profile data into list.

allProfiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]

we will loop through the profile list and check for passwords.

for i in allProfiles:
    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]

finally, check and store the password and print inside try..except.

try:
        print ("{:<30}|  {:<}".format(i, results[0]))
    except IndexError:
        print ("{:<30}|  {:<}".format(i, ""))

Complete code to get Wifi Passwords

Here is complete code to get saved Wifi passwords on a computer.

#wifi.py

import subprocess

data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')

allProfiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]

for i in allProfiles:
    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, ""))

Output:

web_logic_1                   |  web@#fgh@1
web_tuts                      |  mypass@Wlm#
OnePlus            	      |  wifi@pass#$
TP-Link                       |  tp@my#1
my magic                      |  my@top#1
dingding                      |  ding@jk1#@!