python wifi_python 连接wifi脚本

//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秒

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 # 密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值