Principle#
cmd query local wifi configuration#
Execute the command netsh wlan show profiles
in cmd:
It can display all the WLAN configuration files on this computer. To view the password of one of them, specify name=wlan name (SSID) key=clear, for example:
netsh wlan show profiles name=SSID key=clear
Python's os.popen() function executes the passed parameters in cmd#
In Python, there is a file/directory library called os, which has a function called os.popen that can open a pipe from a command.
Introduction to os.popen: python os.popen method | Rookie Tutorial
This command can execute the string passed in cmd, for example os.popen('dir').read()
These are all the basic knowledge used.
Code Explanation#
-
First, import the library:
import os
-
Then, use the os.popen() function to query the total wifi configuration:
cmd_get_allpfs = ('netsh wlan show profiles')
with os.popen(cmd_get_allpfs) as f:
SSID_list = []
for line in f:
if '所有用户配置文件 :' in line:
line = line.strip().split(":")[1]
SSID_list.append(line)
- If the returned content line contains the phrase ' 所有用户配置文件 :' (all user profiles), split this line using ":" and store the second part, which is the wifi name, in the wifi name list SSID_list. Then, we execute the password query statement and print it out one by one:
for SSID in SSID_list:
cmd_get_evepf = ('netsh wlan show profiles name={} key=clear'.format(SSID))
with os.popen(cmd_get_evepf) as r:
evepf = r.read()
print(evepf)
- Format each SSID in the list and pass it into the query statement, then print the query content. The effect is as follows:
Of course, if you just want to simply obtain the SSID and password, for example:
SSID_name1:password1
SSID_name2:password2
You just need to make a simple modification. The following is the complete code to obtain SSID:password
:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author: soapffz
@fucntion: Print all wifi account names and passwords on the computer
@time: 2018-12-15
'''
import os
cmd_get_allpfs = ('netsh wlan show profiles')
with os.popen(cmd_get_allpfs) as f:
SSID_list = []
for line in f:
if '所有用户配置文件 :' in line:
line = line.strip().split(":")[1]
SSID_list.append(line)
PASS_list = []
for SSID in SSID_list:
cmd_get_evepf = ('netsh wlan show profiles name={} key=clear'.format(SSID))
with os.popen(cmd_get_evepf) as r:
for line in r:
if '关键内容' in line:
line = line.strip().split(":")[1]
PASS_list.append(line)
for i in range(len(SSID_list)):
print("{}:{}".format(SSID_list[i], PASS_list[i]))
- The effect is as follows: