目錄
- CMD命令獲取電腦里配置過的wifi信息
- 用python腳本來實現這一操作,并獲取所有配置過的wifi密碼
CMD命令獲取電腦里配置過的wifi信息
設定一個場景,假如我忘記了自家的wifi密碼,這時候小伙伴來家里跟我開黑,問我wifi密碼是多少?我就抓住了這個小秀一波的機會。
上才藝:
按下win+R鍵,輸入CMD打開DOS窗口,然后輸入以下命令查看電腦里配置過的wifi。

這是電腦連過的wifi,假設我家里的wifi是CMCC-CMCC,接下來我需要查看CMCC-CMCC的信息,上代碼:
netsh wlan show profiles CMCC-CMCC

可以看到,安全密鑰這里顯示的是存在,說明這個wifi是有密碼的,接下來,我們用在上面的代碼基礎上加上"key=clear",密碼就會顯示了。(不用擔心這個clear,不會刪除你的wifi密碼的,只是一個顯示作用)
netsh wlan show profiles CMCC-CMCC key=clear

這個就是我的wifi密碼了!
用python腳本來實現這一操作,并獲取所有配置過的wifi密碼
首先導入兩個庫subprocess、re
- subprocess 模塊允許我們啟動一個新進程,并連接到它們的輸入/輸出/錯誤管道,從而獲取返回值。
- re 模塊使 Python 語言擁有全部的正則表達式功能。
我們建立一個通道來執行DOS命令,列出所有的連接過的wifi,并且用profile_names來存下這些WIFI名稱,輸出采用字典的形式,每個wifi名和密碼為一個字典中的鍵值對,并且先定義一個列表用來存下這些字典。
command_output = subprocess.run(['netsh','wlan','show','profiles'],capture_output= True).stdout.decode(encoding='gbk')
profile_names = re.findall('所有用戶配置文件 :(.*)\r',command_output)#返回一個列表
wifi_list=list()
將下一個命令即“查看wifi是否存在密鑰”寫進代碼
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')
如果安全密鑰的內容不是存在的話,說明wifi是沒有密碼的,我們就不用去瞎搞了,直接continue了。如果是存在的話,我們才進行下一步操作,就是進行下一個命令:加上key=clear,查看密碼。這里解釋一下為什么要用name[1:],可能是因為編碼的原因,通過正則得來的name前面包含了一個空格,如果直接用這個name的話,電腦會告訴你沒有這個wifi。例如上文中,我的wifi是“CMCC-CMCC”,當是通過正則獲取的wifi名就是“ CMCC-CMCC”多了一個空格。電腦會以為這兩個東西它不一樣。。。
if re.search('安全密鑰 : 不存在',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')
password = re.search('關鍵內容 :(.*)\r',profile_info_pass)
if password == None:
wifi_profile["password"]=None
else:
wifi_profile["password"] = password[1]
wifi_list.append(wifi_profile)
最后,只要把列表(wifi_list)中的內容打印出來就行了,我把所有代碼跟在后面,方便大家理解。
import subprocess
import re
command_output = subprocess.run(['netsh','wlan','show','profiles'],capture_output= True).stdout.decode(encoding='gbk')
profile_names = re.findall('所有用戶配置文件 :(.*)\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('安全密鑰 : 不存在',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')
password = re.search('關鍵內容 :(.*)\r',profile_info_pass)
if password == None:
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])
如果大家有什么高見請批評指正
以上就是如何利用python和DOS獲取wifi密碼的詳細內容,更多關于用python和DOS獲取wifi密碼的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- 使用PyQt5設計GUI實現程序圖形界面設計
- 如何用Python破解wifi密碼過程詳解
- Python爬取破解無線網絡wifi密碼過程解析
- Python腳本暴力破解柵欄密碼
- python攻防-破解附近局域網WIFI密碼實現上網自由