python爬取wifi密码完整代码_pywifi

pywifi

pywifi.svg

pywifi.svg?branch=master

pywifi.svg

pywifi provides a cross-platform Python module for manipulating wireless

interfaces.

Easy to use

Supports Windows and Linux

Prerequisites

On Linux, you will need to run wpa_supplicant to manipulate the wifi devices,

and then pywifi can communicate with wpa_supplicant through socket.

On Windows, the Native Wifi component comes with Windows versions greater

than Windows XP SP2.

Installation

After installing the prerequisites listed above for your platform, you can

use pip to install from source:

cd pywifi/

pip install .

Documentation

For the details of pywifi, please refer to Documentation.

Example

import time

import pywifi

from pywifi import const

wifi = pywifi.PyWiFi()

iface = wifi.interfaces()[0]

iface.disconnect()

time.sleep(1)

assert iface.status() in\

[const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

profile = pywifi.Profile()

profile.ssid = 'testap'

profile.auth = const.AUTH_ALG_OPEN

profile.akm.append(const.AKM_TYPE_WPA2PSK)

profile.cipher = const.CIPHER_TYPE_CCMP

profile.key = '12345678'

iface.remove_all_network_profiles()

tmp_profile = iface.add_network_profile(profile)

iface.connect(tmp_profile)

time.sleep(30)

assert iface.status() == const.IFACE_CONNECTED

iface.disconnect()

time.sleep(1)

assert iface.status() in\

[const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

(C) Jiang Sheng-Jhih 2019, MIT License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个使用Python爬取WiFi密码的示例代码: ```python import os def get_wifi_password(): if os.name == 'posix': # for Mac and Linux wifi_name_output = os.popen('security find-generic-password -wa "Wi-Fi"').read() wifi_password = wifi_name_output.rstrip("\n") elif os.name == 'nt': # for Windows wifi_password_output = os.popen('netsh wlan show profile name="Wi-Fi" key=clear').read() wifi_password_lines = wifi_password_output.split("\n") for line in wifi_password_lines: if "Key Content" in line: wifi_password = line.split(":")[1].strip() else: wifi_password = "Unsupported OS" return wifi_password password = get_wifi_password() print("Wi-Fi密码为:" + password) ``` 这段代码首先判断操作系统类型,然后根据不同的操作系统执行不同的命令来获取WiFi密码。对于Mac和Linux,使用`security find-generic-password`命令来获取密码;对于Windows,使用`netsh wlan show profile name="Wi-Fi" key=clear`命令来获取密码。 请注意:该代码仅适用于特定的操作系统(Mac、Linux、Windows),若要在其他操作系统上运行,请根据实际情况修改命令。另外,代码执行需要相应的权限,例如在Windows上需要以管理员权限运行。 ### 回答2: Python爬取WiFi密码完整代码如下: ``` import subprocess import re def get_wifi_password(): # 使用subprocess模块运行系统命令,获取WiFi密码列表 command_output = subprocess.run(['netsh', 'wlan', 'show', 'profiles'], capture_output=True).stdout.decode('UTF-8') # 使用正则表达式匹配WiFi名称 wifi_names = re.findall(r'All User Profile : (.*)\r', command_output) wifi_passwords = [] # 遍历WiFi名称列表,逐个获取对应的WiFi密码 for name in wifi_names: command_output = subprocess.run(['netsh', 'wlan', 'show', 'profile', name, 'key=clear'], capture_output=True).stdout.decode('UTF-8') # 使用正则表达式匹配WiFi密码 password = re.findall(r'Key Content : (.*)\r', command_output) if len(password) > 0: wifi_passwords.append({'SSID': name, 'Password': password[0]}) return wifi_passwords # 使用get_wifi_password函数获取WiFi密码列表 passwords = get_wifi_password() # 打印WiFi密码列表 for password in passwords: print(f"WiFi名称: {password['SSID']}") print(f"密码: {password['Password']}") print('--------') ``` 此代码使用了subprocess模块来运行系统命令,通过`netsh wlan show profiles`命令获取所有的WiFi名称,然后逐个使用`netsh wlan show profile {WiFi名称} key=clear`命令获取对应的WiFi密码。通过正则表达式提取出密码后,将WiFi名称和密码以字典的形式存储在列表中,并最后将列表输出显示出来。 ### 回答3: Python爬取WiFi密码完整代码如下所示: ```python import subprocess # 使用命令行获取WiFi密码 def get_wifi_password(): try: # 使用subprocess模块调用cmd命令 output = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('gbk').split('\n') profiles = [i.split(":")[1][1:-1] for i in output if "所有用户配置文件" in i] wifi_passwords = [] for profile in profiles: try: # 使用subprocess模块调用cmd命令 results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', profile, 'key=clear']).decode('gbk').split('\n') results = [b.split(":")[1][1:-1] for b in results if "关键内容" in b] wifi_passwords.append("{:<30}| {}".format(profile, results[0])) except subprocess.CalledProcessError: wifi_passwords.append("{:<30}| {}".format(profile, "密码不可用")) return wifi_passwords except subprocess.CalledProcessError: return "无法获取WiFi密码" # 打印获取的WiFi密码 def print_wifi_password(results): if isinstance(results, list): for wifi_password in results: print(wifi_password) else: print(results) # 主函数 if __name__ == "__main__": wifi_passwords = get_wifi_password() print_wifi_password(wifi_passwords) ``` 这段代码使用`subprocess`模块调用命令行来获取WiFi密码。首先使用`netsh wlan show profiles`命令获取计算机中保存的所有WiFi配置文件,然后使用`netsh wlan show profile <profile_name> key=clear`命令来获取每个配置文件的密码。使用`subprocess.check_output()`函数执行命令,并使用`decode()`方法将输出解码为UTF-8字符串。最后,将获取的WiFi密码整理为列表形式,并打印出来。 请注意,该代码只能在Windows操作系统上运行,并且需要以管理员权限运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值