所有文章除特別聲明外,均采用 CC BY-NC-SA 4.0 許可協(xié)議。轉(zhuǎn)載請注明來自 nixgnauhcuy's blog!
如需轉(zhuǎn)載,請標明出處!
技術(shù)是描述某種尚未發(fā)揮作用的東西的詞匯. —— Douglas Adams
前言
??鴿王回歸,鴿了好久沒發(fā)文章,難得有空,制作點簡單的小工具!
原理
制作工具前,我們先了解下獲取已連接過的 WIFI 賬號密碼的原理。
win+R 打開我們的 cmd 命令行窗口,輸入 netsh wlan show network,可以獲取當前網(wǎng)絡接口下可連接的所有 WIFI 名稱,如果想更多的信息,則輸入 netsh wlan show networks mode=bssid,來顯示當前周邊有效無線網(wǎng)絡的相關(guān)信息(網(wǎng)卡地址、加密類型、信號強度、無線電類型、占用頻道、基本速率等信息)。


但是這些信息還不包括我們需要的 WIFI 名稱及密碼,所以我們再進一步,輸入 netsh wlan show profiles,顯示當前本機保存的 profiles,隨后再單獨對某一個配置文件進行查詢,如圖片中的 nixgnauhcuy,輸入 netsh wlan show profiles nixgnauhcuy key=clear,在安全設置中的關(guān)鍵內(nèi)容,可以看到我已連接的 WIFI nixgnauhcuy 的密碼 123456789。


通過上面的方式,我們就查到了單個 WIFI 的信息,那么剩下的就是將其遍歷,獲取本機下所有已連接設備的賬號密碼。
UI 界面
原理我們已經(jīng)琢磨清楚了,那么我們就先來設計我們工具的界面 UI,再來寫邏輯。(因為是做工具,所以我才會多此一舉,做多一步,需要的可以熟悉熟悉 QT,不需要的可以跳到下一步)
UI 界面我還是用 Qt Designer 來設計。界面功能比較簡單,二個 Button 控件及一個 TabView 控件來實現(xiàn),點擊 Button 后獲取本機下已連接賬號和密碼顯示在 TabView 中,然后另一個 Button 將 TabView 的內(nèi)容輸出到表格中。

功能實現(xiàn)
因為原理相同,并且功能簡單,所以將 UI 工具代碼和不帶 UI 的代碼一起完成。
UI 版
將做好的 UI 文件保存到工程下,這里我將文件命名為 wifi_tool.ui,隨后將 .ui 文件轉(zhuǎn)成 Ui_wifi_tool.py.
Ui_wifi_tool.py
# -*- 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 代碼如下:
# -*- 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_())
運行效果如下:

開始編寫我們的邏輯:
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("關(guān)鍵內(nèi)容 : (.*)\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 模塊,執(zhí)行我們上述原理的指令,捕獲指令執(zhí)行后的輸出,用正則表達式取出我們需要的內(nèi)容,也就是賬號及其密碼,其次,還將賬號密碼寫入 TabView 和表格中。
最后增加保存控件的邏輯,也就是保存賬號密碼的內(nèi)容到表格 wifi_key.xls 中。
self.saveButton.clicked.connect(self.save_wifi_info_butt)
def save_wifi_info_butt(self):
self.workbook.save('wifi_key.xls')


非 UI 版
非 UI 版邏輯同上,只是少了界面控件的操作,具體代碼如下:
# -*- 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("關(guān)鍵內(nèi)容 : (.*)\r", profile_info))
print('name = ' + wifi_name + ', password =', key[0])

結(jié)語
關(guān)于本篇的相關(guān)代碼已經(jīng)上傳到 github 上去了,有興趣的可以訪問 python_wifi_tool 查看,瀏覽的同時也可以在該 github 倉庫中點個 Star 支持我一下??。
與本文相關(guān)的內(nèi)容還有:
python 開發(fā)環(huán)境搭建
python 制作串口工具(一)
python 制作串口工具(二)