前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 连接wifi脚本

python 连接wifi脚本

作者头像
用户5760343
发布2022-05-14 14:35:31
1.2K0
发布2022-05-14 14:35:31
举报
文章被收录于专栏:sktj

//pip install pywifi import pywifi,time from pywifi import const import subprocess

def wifi_connect_status(): """ 判断本机是否有无线网卡,以及连接状态 :return: 已连接或存在无线网卡返回1,否则返回0 """ #创建一个元线对象 wifi = pywifi.PyWiFi() #取当前机器,第一个元线网卡 iface = wifi.interfaces()[0] #有可能有多个无线网卡,所以要指定 #判断是否连接成功 if iface.status() in [const.IFACE_CONNECTED,const.IFACE_INACTIVE]: print('wifi已连接') return 0 else: print('wifi未连接') return 1 def connect_wifi(): wifi = pywifi.PyWiFi() # 创建一个wifi对象 ifaces = wifi.interfaces()[0] # 取第一个无限网卡 ifaces.disconnect() # 断开网卡连接 time.sleep(3) # 缓冲3秒

代码语言:javascript
复制
profile = pywifi.Profile()  # 配置文件
profile.ssid = "802.1x"  # wifi名称

ifaces.remove_all_network_profiles()  # 删除其他配置文件
tmp_profile = ifaces.add_network_profile(profile)  # 加载配置文件

ifaces.connect(tmp_profile)  # 连接
time.sleep(6)  # 尝试6秒能否成功连接
if ifaces.status()==const.IFACE_CONNECTED:
    print("802.1x连接成功")
else:
    print("802.1x连接失败")

查看当前连接的是哪个WIFI

def get_wifi_name(): status, output =subprocess.getstatusoutput("netsh WLAN show interfaces") if output.find("802.1x")!=-1: print("当前连接的wifi是802.1x") return 0 else: print("当前连接的wifi不是802.1x") return 1 def con801(): if(wifi_connect_status()==1): connect_wifi() else: if(get_wifi_name()==1): connect_wifi() while True: con801() time.sleep(10)

//https://blog.csdn.net/xiaoxianerqq/article/details/81279425 //密码方式: profile.akm.append(const.AKM_TYPE_WPA2) # wifi加密算法 profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元 profile.key = pwd # 密码

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查看当前连接的是哪个WIFI
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档