windows命令枚举wifi,连接wifi

枚举

netsh wlan show all | findstr "^\s*SSID"

netsh wlan show all 显示所有网络连接,findstr过滤

连接

netsh wlan connect name="tplink-4jt454ktj"
多网卡的情况下指定用那块网卡链接
(普通pc只能连接2.4G赫兹的wifi,外接一个无线网卡可以连接上5G赫兹平频率的wifi,而网卡也就多了一个)
netsh wlan connect name="tplink-4jt454ktj" interface="WLAN 2"

需要手动连接一次,并且保存密码,如果本地运行网络访问的脚本,判断网络连接异常后可以调用此命令自动重连

python示例:

import requests
import json
import time
import sys
import os

wifiName = "tplink-4jt454ktj"

class convertWork:
	def __init__(self):
		self.data = []
		
	def connectWifi(self):
		os.system("netsh wlan connect name=\"%s\"" % wifiName)
		
	def getFiles(self, url, postData):
		
		try:
		
			response = requests.post(url, data=postData)
			
			data = json.loads(response.content)
					
			if data['errorCode'] != 0:
				print (time.strftime("%Y-%m-%d %H:%M:%S"), url, 'failed')
			
			return data
		except Exception as e:
			print (time.strftime("%Y-%m-%d %H:%M:%S"), "getFiles get except ", e)
			self.connectWifi()
			return {}

if __name__ == "__main__":
	
	if len(sys.argv) > 1 and re.match('(http|https)://.*\.com$', sys.argv[1], re.I):
		print ("change host name to", sys.argv[1])
		hostName = sys.argv[1]

	print (time.strftime("%Y-%m-%d %H:%M:%S"), "begin", hostName)
    #do something you want

需要while死循环执行的时候,经常会遇到wifi断开的情况,windows可能不会自动重连,如果自己不拯救自己的程序,后续就没法进行了

发布了275 篇原创文章 · 获赞 46 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/youyudexiaowangzi/article/details/96476704