Windows10/11下python脚本自动连接WiFi热点

直接上代码:

注意:最后的结果可能会报错,不过,没关系,连上了就行。

import pywifi
from pywifi import const
import time


def connect_wifi(ssid, password):
    wifi = pywifi.PyWiFi()
    iface = wifi.interfaces()[0]

    iface.disconnect()
    profile = pywifi.Profile()
    profile.ssid = ssid
    profile.auth = const.AUTH_ALG_OPEN
    profile.akm.append(const.AKM_TYPE_WPA2PSK)
    profile.cipher = const.CIPHER_TYPE_CCMP
    profile.key = password

    iface.remove_all_profiles()
    tmp_profile = iface.add_profile(profile)

    iface.connect(tmp_profile)

    while True:
        time.sleep(5)
        if iface.status() == const.IFACE_CONNECTED:
            print(f"成功连接到WiFi:{ssid}")
            break
        elif iface.status() == const.IFACE_DISCONNECTED:
            print(f"连接WiFi:{ssid} 失败,请检查密码或网络设置。")
            break


if __name__ == "__main__":
    wifi_ssid = "xxxxx"
    wifi_password = "xxxxxxx"
    connect_wifi(wifi_ssid, wifi_password)

关于Windows10下连接WiFi,如果你是直接使用netsh命令,那么是需要配置文件的,配置文件中是可以使用明文密码而不是加密密文的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1号程序媛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值