python 获取已连接wifi密码工具

技术是描述某种尚未发挥作用的东西的词汇. —— Douglas Adams

前言

😄鸽王回归,鸽了好久没发文章,难得有空,制作点简单的小工具!


原理

制作工具前,我们先了解下获取已连接过的 WIFI 账号密码的原理。

win+R 打开我们的 cmd 命令行窗口,输入 netsh wlan show network,可以获取当前网络接口下可连接的所有 WIFI 名称,如果想更多的信息,则输入 netsh wlan show networks mode=bssid,来显示当前周边有效无线网络的相关信息(网卡地址、加密类型、信号强度、无线电类型、占用频道、基本速率等信息)。

但是这些信息还不包括我们需要的 WIFI 名称及密码,所以我们再进一步,输入 netsh wlan show profiles,显示当前本机保存的 profiles,随后再单独对某一个配置文件进行查询,如图片中的 nixgnauhcuy,输入 netsh wlan show profiles nixgnauhcuy key=clear,在安全设置中的关键内容,可以看到我已连接的 WIFI nixgnauhcuy 的密码 123456789

通过上面的方式,我们就查到了单个 WIFI 的信息,那么剩下的就是将其遍历,获取本机下所有已连接设备的账号密码。


UI 界面

原理我们已经琢磨清楚了,那么我们就先来设计我们工具的界面 UI,再来写逻辑。(因为是做工具,所以我才会多此一举,做多一步,需要的可以熟悉熟悉 QT,不需要的可以跳到下一步)

UI 界面我还是用 Qt Designer 来设计。界面功能比较简单,二个 Button 控件及一个 TabView 控件来实现,点击 Button 后获取本机下已连接账号和密码显示在 TabView 中,然后另一个 Button 将 TabView 的内容输出到表格中。

功能实现

因为原理相同,并且功能简单,所以将 UI 工具代码和不带 UI 的代码一起完成。

UI 版

将做好的 UI 文件保存到工程下,这里我将文件命名为 wifi_tool.ui,随后将 .ui 文件转成 Ui_wifi_tool.py.

Ui_wifi_tool.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(432, 270)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableView = QtWidgets.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(20, 60, 391, 192))
self.tableView.setObjectName("tableView")
self.getButton = QtWidgets.QPushButton(self.centralwidget)
self.getButton.setGeometry(QtCore.QRect(20, 20, 75, 23))
self.getButton.setObjectName("getButton")
self.saveButton = QtWidgets.QPushButton(self.centralwidget)
self.saveButton.setGeometry(QtCore.QRect(340, 20, 75, 23))
self.saveButton.setObjectName("saveButton")
MainWindow.setCentralWidget(self.centralwidget)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "wifi tool"))
self.getButton.setText(_translate("MainWindow", "获取"))
self.saveButton.setText(_translate("MainWindow", "保存"))

随后在工程中编写我们的 main_ui.py,运行看看效果,

main_ui.py 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-
import sys

from Ui_wifi_tool import Ui_MainWindow
from PyQt5.QtWidgets import (QApplication, QMainWindow)

class MyPyQT_Form(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)

if __name__ == '__main__':
app = QApplication(sys.argv)
my_pyqt_form = MyPyQT_Form()
my_pyqt_form.show()
sys.exit(app.exec_())

运行效果如下:

开始编写我们的逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
self.getButton.clicked.connect(self.get_wifi_info_butt)

def get_wifi_info_butt(self):
# 获取本机下存在的 profile
profiles = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output=True).stdout.decode('gbk')

# 将 profile 中的 wifi 名取出
wifi_names = (re.findall("所有用户配置文件 : (.*)\r", profiles))
row = 0
for wifi_name in wifi_names:
column = 0
item=QStandardItem('%s'% wifi_name)
self.model.setItem(row, column, item)
self.worksheet.write(row, column, wifi_name)

# 获取 profile 中的密码
profile_info = subprocess.run(["netsh", "wlan", "show", "profiles", wifi_name, "key=clear"], capture_output=True).stdout.decode('gbk')

# 将 profile 中的 wifi 密码取出
key = (re.findall("关键内容 : (.*)\r", profile_info))
column += 1
item=QStandardItem('%s'% str(key[0]))
self.model.setItem(row, column, item)
self.worksheet.write(row, column, str(key[0]))
row += 1

上面主要运用 subprocess 模块,执行我们上述原理的指令,捕获指令执行后的输出,用正则表达式取出我们需要的内容,也就是账号及其密码,其次,还将账号密码写入 TabView 和表格中。

最后增加保存控件的逻辑,也就是保存账号密码的内容到表格 wifi_key.xls 中。

1
2
3
4
self.saveButton.clicked.connect(self.save_wifi_info_butt)

def save_wifi_info_butt(self):
self.workbook.save('wifi_key.xls')

非 UI 版

非 UI 版逻辑同上,只是少了界面控件的操作,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding: utf-8 -*-
import subprocess
import re

if __name__ == '__main__':
# 获取本机下存在的 profile
profiles = subprocess.run(["netsh", "wlan", "show", "profiles"], shell=False, capture_output=True).stdout.decode('gbk')

# 将 profile 中的 wifi 名取出
wifi_names = (re.findall("所有用户配置文件 : (.*)\r", profiles))
for wifi_name in wifi_names:

# 获取 profile 中的密码
profile_info = subprocess.run(["netsh", "wlan", "show", "profiles", wifi_name, "key=clear"], shell=False, capture_output=True).stdout.decode('gbk')

# 将 profile 中的 wifi 密码取出
key = (re.findall("关键内容 : (.*)\r", profile_info))

print('name = ' + wifi_name + ', password =', key[0])

结语

关于本篇的相关代码已经上传到 github 上去了,有兴趣的可以访问 python_wifi_tool 查看,浏览的同时也可以在该 github 仓库中点个 Star 支持我一下😄。

与本文相关的内容还有:
python 开发环境搭建
python 制作串口工具(一)
python 制作串口工具(二)