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)

//密码方式:

profile.akm.append(const.AKM_TYPE_WPA2) # wifi加密算法

profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元

profile.key = pwd # 密码

//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) //密码方式: profile.akm.append(const.AKM_TYPE_WPA2) # wifi加密算法 profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元 profile.key = pwd # 密码
经验分享 程序员 微信小程序 职场和发展