• CMD command to obtain the wifi password
  • Obtain connected wifi passwords in batches through Python

CMD command to obtain the wifi information configured on the PC

Set a scene, if I forget my wifi password, at this time my friend comes to my home and asks me how much wifi password is? I jumped at the chance to do a little show.

Talent:

This is the WIFI connected to the computer. Suppose the wifi at my home is McCc-cmcc. Next, I need to check the information of McCc-cmcc.

netsh wlan show profiles CMCC-CMCC
Copy the code

It can be seen that the security key is displayed here, indicating that the wifi has a password. Next, we add “key=clear” on the basis of the above code, and the password will be displayed. (Don’t worry about this clear, it won’t delete your wifi password, it’s just a display)

netsh wlan show profiles CMCC-CMCC key=clear =

This is my wifi password!

Do this with a Python script and get all configured wifi passwords

First import two libraries subProcess and RE

  • The subprocess module allows us to start a new process and connect to their input/output/error pipes to get the return value.

  • The RE module gives the Python language full regular expression functionality.

We set up a channel to execute the DOS command that lists all connected wifi names and stores them with profile_names. The output takes the form of a dictionary, with each wifi name and password as a key/value pair in the dictionary, and defines a list to store these dictionaries.

command_output = subprocess.run(['netsh','wlan','show','profiles'],capture_output= True).stdout.decode(encoding='gbk') Profile_names = re.findall(' all user profiles :(.*)\r',command_output)# return a list wifi_list=list()Copy the code

Code the next command, “See if wifi has a key.

if len(profile_names) ! = 0: for name in profile_names: wifi_profile = dict() profile_info = subprocess.run(['netsh','wlan','show','profiles',name],capture_output=True).stdout.decode(encoding='gbk')Copy the code

If the content of the security key does not exist, it means that wifi does not have a password, we do not need to mess with it, directly continue. If so, we proceed to the next step, which is the next command: add key=clear to check the password. Here is an explanation of why name[1:] is used. It may be because of coding reasons. The name obtained through the re contains a space in front of it. For example, my wifi is “CMCC-cmcc”, and the wifi name obtained through the re is “CMCC-cmcc” with one more space. The computer thinks these two things are different…

If re.search(' security key: not present ',profile_info): continue else: wifi_profile['ssid'] = name[1:] profile_info_pass = subprocess.run(['netsh','wlan','show','profiles',name[1:],'key=clear'],capture_output=True).stdout.decode(encoding='gbk' Profile_info_pass) if password == None: wifi_profile["password"]=None else: wifi_profile["password"]=None else: wifi_profile["password"] = password[1] wifi_list.append(wifi_profile)Copy the code

Finally, just print out the contents of the wifi_list, and I’ll follow all the code so you can understand it.

import subprocess import re command_output = subprocess.run(['netsh','wlan','show','profiles'],capture_output= True).stdout.decode(encoding=' GBK ') profile_names = re.findall(' all user profiles :(.*)\r',command_output) wifi_list=list() print(type(profile_names)) if len(profile_names) ! = 0: for name in profile_names: wifi_profile = dict() profile_info = subprocess.run(['netsh','wlan','show','profiles',name],capture_output=True).stdout.decode(encoding='gbk') if Re.search (' security key: not present ',profile_info): continue else: wifi_profile['ssid'] = name[1:] profile_info_pass = subprocess.run(['netsh','wlan','show','profiles',name[1:],'key=clear'],capture_output=True).stdout.decode(encoding='gbk' Profile_info_pass) if password == None: wifi_profile["password"]=None else: wifi_profile["password"]=None else: wifi_profile["password"] = password[1] wifi_list.append(wifi_profile) for x in range(len(wifi_list)): print(wifi_list[x])Copy the code

If you have any comments please criticize

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base