python实现wifi链接
·
import pywifi
from pywifi import const # 引用一些常量
import time
import fileinput
# 判断是否已经连接wifi
def gic():
wifi = pywifi.PyWiFi() # 创建一个无线对象
iface = wifi.interfaces()[0] # 获取到无线网卡列表,取第一个
# 网卡的名称 iface.name()
# 网卡的链接状态 ifce.status() 0未链接 4链接
# 判断网卡已经链接
if iface.status() == const.IFACE_CONNECTED:
print('已连接')
else:
print('未连接')
# 扫描附件的wifi
def scanWifi():
wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
iface.scan()# 扫描wifi
time.sleep(5) # 等待扫描时间
result = iface.scan_results()
print(f'WIFI数量:{len(result)}')
for index,wifi_info in enumerate(result):
print(f'{index}{wifi_info.ssid}')
# 链接wifi
def linkWifi(wifiName):
# 抓取网卡接口
wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
# 断开所有wifi
time.sleep(1)
wifiStatus = iface.status()
if wifiStatus == const.IFACE_CONNECTED: #状态等于已连接
iface.disconnect()
time.sleep(2)
profile = pywifi.Profile() # 创建wifi连接文件
profile.ssid = wifiName # 设置wifi名称
profile.auth = const.AUTH_ALG_OPEN # 开放网卡
profile.akm.append(const.AKM_TYPE_WPA2PSK) # 设置wifi的加密算法
profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元
wifiConnect(profile,iface)
def wifiConnect(profile,iface):
for lines in fileinput.input(['E:/www/toy/pwd.txt']):
profile.key = lines
iface.remove_all_network_profiles() # 删除其他配置文件
tmp_profile = iface.add_network_profile(profile)
iface.connect(tmp_profile) # 连接
time.sleep(3) # 等待连接结果
if iface.status() == const.IFACE_CONNECTED:
print(f'------------------连接成功:{profile.key}')
break
else:
print(profile.key)
linkWifi('HUAWEI_B535_B68E')
更多推荐
所有评论(0)