python 獲取已連接wifi密碼工具

個人博客

所有文章除特別聲明外,均采用 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)卡地址、加密類型、信號強度、無線電類型、占用頻道、基本速率等信息)。

image
image

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

image
image

通過上面的方式,我們就查到了單個 WIFI 的信息,那么剩下的就是將其遍歷,獲取本機下所有已連接設備的賬號密碼。


UI 界面

原理我們已經(jīng)琢磨清楚了,那么我們就先來設計我們工具的界面 UI,再來寫邏輯。(因為是做工具,所以我才會多此一舉,做多一步,需要的可以熟悉熟悉 QT,不需要的可以跳到下一步)

UI 界面我還是用 Qt Designer 來設計。界面功能比較簡單,二個 Button 控件及一個 TabView 控件來實現(xiàn),點擊 Button 后獲取本機下已連接賬號和密碼顯示在 TabView 中,然后另一個 Button 將 TabView 的內(nèi)容輸出到表格中。

image

功能實現(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_())

運行效果如下:

image

開始編寫我們的邏輯:

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')
image
image

非 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])
image

結(jié)語

關(guān)于本篇的相關(guān)代碼已經(jīng)上傳到 github 上去了,有興趣的可以訪問 python_wifi_tool 查看,瀏覽的同時也可以在該 github 倉庫中點個 Star 支持我一下??。

與本文相關(guān)的內(nèi)容還有:
python 開發(fā)環(huán)境搭建
python 制作串口工具(一)
python 制作串口工具(二)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Windows下netsh wlan命令 @Date 2017.04.18 windows系統(tǒng)下,CMD命令獲取已...
    voltric閱讀 739評論 0 0
  • 一般人忘記已連接的wifi密碼操作:打開控制面板\網(wǎng)絡和 Internet\網(wǎng)絡和共享中心 點連接:WLAN(wi...
    DragonersLi閱讀 2,161評論 0 0
  • CMD平常人看起來根本沒什麼用,但是如果你會使用,你會發(fā)現(xiàn):它真的很強大! Windows 8、Windows 8...
    VI8080閱讀 805評論 0 2
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,814評論 28 54
  • 人工智能是什么?什么是人工智能?人工智能是未來發(fā)展的必然趨勢嗎?以后人工智能技術(shù)真的能達到電影里機器人的智能水平嗎...
    ZLLZ閱讀 4,085評論 0 5

友情鏈接更多精彩內(nèi)容