Loading, please wait...

A to Z Full Forms and Acronyms

How to See Connected Wifi Password in python | Subprocess

This article demonstrates how we can use the subprocess module to create new process in Python and using this module how we can see the password of connected WIFI.

How to see the password of connected wifi using python?

Python comes with lots of handy and easily accessible libraries. Through this article, we will learn how we can see the password

of all connected wifi to our system using python. Here we are not creating any GUI application or anything else we will just use

one library that will help to do so. 

For this purpose, python provides a unique library called subprocess. This helps to check the connected wifi password easily by allowing us to run command prompt commands inside

the program. Topics that we will discuss in this article are mentioned below:

  • About Module
  • Module Installation and importing
  • Command prompt commands that we are using in our program
  • Code
  • Output

About Module:

Here, we are using the “subprocess” module. This module is supported in both 2.x and 3.x. this is used to run the new programs and applications through python code by creating new processes. It can be used to get the input/output/errors

And exit codes. If we want to execute different programs then we can use the functions of the subprocess.

There are two python functions of subprocess lets discuss them.

1.subprocess.check_call(args,*,stdin=None,shell=False):

This returns the return code of the command. If return code is zero then function retruns(cmd executed successfully)

Otherwise error will be raised.

2.subprocess.check_output(args,*,stdin=None,stderr=None ,shells=False, universal_newlines=False):

This returns the return code of the command. If return code is zero then function retruns byte string(cmd executed successfully) otherwise error will be raised.

Module Installation and importing:

 The syntax for installation:

pip install subprocess

The syntax for importing:

import subprocess

Command prompt commands that we are using in our program:

  1. netsh wlan show profile

        This command is used to show the profile of the connected Wifi.     

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

        This command is used to show the password of the wifi Which you want to know.

In our code, we will use the above-mentioned command in our program. Now let's discuss the code to see the password of the

Connected wifi.

CODE:

# first we will import the subprocess module
import subprocess

# now we will store the profiles data in "data" variable by
# running the 1st cmd command using subprocess.check_output
data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')

# now we will store the profile by converting them to list
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]

# using for loop in python we are checking and printing the wifi
# passwords if they are available using the 2nd cmd command
for i in profiles:
    # running the 2nd cmd command to check passwords
    password= subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i,
                        'key=clear']).decode('utf-8').split('\n')
    # storing passwords after converting them to list
    password = [b.split(":")[1][1:-1] for b in password if "Key Content" in b]
    # printing the profiles(wifi name) with their passwords using
    # try and except method for exception handing
    try:
        print ("{:<30}|  {:<}".format(i, password[0]))
    except IndexError:
        print ("{:<30}|  {:<}".format(i, ""))

OUTPUT:

Hope you like this article, if there is any query related to python or any modules then feel free to comment. Share this with your friends if you find it useful. In our upcoming article, we will create a GUI application for the same.

Till now, Stay tuned and keep on learning😊😊

A to Z Full Forms and Acronyms

Related Article