无线网卡的批量连接脚本

Zss 发表于:

在工作中经常和wifi所打交道,有时候需要复现一些问题,而这些问题是需要大量的客户端的接入可能所导致的,可能七八十上百个

在这种情况下一个个ssid搜索来输入密码连接是比较花时间的,所以写一个脚本来控制这些无线网卡的ip设置和ssid的连接

目的:

1.设置全部为dhcp获取方式,让电脑中所有的无线网卡连接某个ssid

2.设置全部为静态ip,让电脑中所有的无线网卡连接某个ssid

3.设置全部为静态ip,且设置一个ip来让其ping,如果ping不通了,重启网卡再次连接,直到正常ping通

4.不做任何设置让电脑中所有的无线网卡连接某个ssid

这中间核心点是使用win下面的cmd命令控制wifi的连接和ip的设置,最开始以为直接使用命令就可连接其实不然,需要一个xml文件来连接,

修改xml文件就需要使用到import xml.etree.ElementTree as ET,需要修改xml文件中的信息来连接不同的ssid

及其加密方式和密码,ssid名都在xml文件中,ssid是以acill码转化为16进制存在xml文件中

还有一点值得注意的是需要使用管理员权限来运行,不然设置ip时没有权限就导致设置失败

无线网卡连接

#coding:gbk
import os,time,sys
import xml.etree.ElementTree as ET
reload(sys)
sys.setdefaultencoding('gbk')

class Auto_connect():
    def get_cardname(self): #获取无线网卡的名字
        card_list = []
        card_name = os.popen('netsh wlan show network').read()
        card_name = card_name.splitlines()
        for i in card_name:
            if '接口名称' in i:
                card_list.append(i.split(':')[1].strip())
        return card_list

    def set_ip(self,card_list,choice):
        name_ip = {}
        if choice == '1':
            for i in card_list:
                os.popen('netsh interface ip set address name="{}" source=dhcp'.format(i))
                #print('netsh interface ip set address name="{}" source=dhcp'.format(i))
            print('------------------------------------------------------------')
            print('              所有无线网卡已设置为自动获取')
            print('------------------------------------------------------------')

        if choice == '2' or choice == '3':
            ip,mask = raw_input('5.输入IP地址 子网掩码,ip的尾数会依次增加1应用在每张网卡\n例如:10.0.1.1 255.255.0.0\n').split()
            print('------------------------------------------------------------')
            for i in card_list:
                name_ip[i] = ip
                print('---> 无线接口:{}  IP设置为:{}   子网掩码设置为:{}'.format(i,ip,mask))
                a = os.popen('netsh interface ip set address "{}" static {} {} '.format(i,ip,mask)).read()
                #print('netsh interface ip set address "{}" static {} {} '.format(i,ip,mask))
                ip = ip.split('.')[0]+'.'+ip.split('.')[1]+'.'+ip.split('.')[2]+'.'+str(int(ip.split('.')[3])+1)
            print('------------------------------------------------------------')
            return name_ip

    def get_ssid(self):
        print('--------------------  批量连接SSID设置  --------------------')
        ssid = raw_input('1.输入需要连接的ssid\n')
        security = raw_input('2.输入加密方式,0为open,1为wpa2-psk\n')
        if security == '1':
            password = raw_input('3.输入密码\n')
            wlan = (ssid,security,password)
            return wlan
        else:
            wlan = (ssid,security)
            return wlan

    def write_xml(self,wlan):
        if wlan[1]=='1':
            tree = ET.parse('psk.xml')
            root = tree.getroot()
            ssid = wlan[0].encode('hex')
            root[1][0][0].text = ssid
            root[1][0][1].text = wlan[0]
            root[4][0][1][2].text = wlan[2]
            root[0].text = wlan[0]
            tree.write('psk.xml')
        else:
            tree = ET.parse('open.xml')
            root = tree.getroot()
            ssid = wlan[0].encode('hex')
            root[1][0][0].text = ssid
            root[1][0][1].text = wlan[0]
            root[0].text = wlan[0]
            tree.write('open.xml')

    def connect_wifi(self,card_name,wlan):
        print('---> 无线网卡:{}   正在连接SSID:{}'.format(card_name,wlan[0]))
        if wlan[1] == '1':
            os.popen('netsh interface set interface "{}"disabled'.format(card_name))
            time.sleep(1)
            os.popen('netsh interface set interface "{}"enabled'.format(card_name))
            os.popen('netsh wlan add profile filename="psk.xml" interface="{}"'.format(card_name))
            os.popen('netsh wlan connect name="{}" interface="{}"'.format(wlan[0],card_name))
        else:
            os.popen('netsh interface set interface "{}"disabled'.format(card_name))
            time.sleep(1)
            os.popen('netsh interface set interface "{}"enabled'.format(card_name))
            os.popen('netsh wlan add profile filename="open.xml" interface="{}"'.format(card_name))
            os.popen('netsh wlan connect name="{}" interface="{}"'.format(wlan[0],card_name))

    def ping_test(self,name_ip,ping_ip):
        fail_card = []
        print('------------------------------------------------------------')
        for name,ip in name_ip.items():
            reslut = os.popen('ping -n 2 -S {} {}'.format(ip,ping_ip)).read()
            if 'TTL='not in reslut:
                print('{} ---> 无线网卡:{} Ping is Fail 准备重启网卡连接对应的SSID'.format(time.strftime("%Y-%m-%d-%X"),name))
                fail_card.append(name)
            else:
                print('{} ---> 无线网卡:{} Ping is PASS'.format(time.strftime("%Y-%m-%d-%X"),name))
        print('------------------------------------------------------------')
        return fail_card

def test1(choice):
    auto = Auto_connect()
    card_list = auto.get_cardname()
    os.popen('netsh wlan delete profile *')
    wlan = auto.get_ssid()
    auto.set_ip(card_list,choice)
    auto.write_xml(wlan)
    for card_name in card_list:
        auto.connect_wifi(card_name,wlan)

def test3(choice):
    auto = Auto_connect()
    card_list = auto.get_cardname()
    os.popen('netsh wlan delete profile *')
    wlan = auto.get_ssid()
    ping_ip = raw_input('4.输入需要ping的地址来确定网卡是否正常\n')
    name_ip = auto.set_ip(card_list,choice)
    auto.write_xml(wlan)
    while 1:
        fail_name = auto.ping_test(name_ip,ping_ip)
        for card_name in fail_name:
            auto.connect_wifi(card_name,wlan)
            time.sleep(1)
        time.sleep(20)

def test4():
    auto = Auto_connect()
    card_list = auto.get_cardname()
    wlan = auto.get_ssid()
    auto.write_xml(wlan)
    for card_name in card_list:
        auto.connect_wifi(card_name,wlan)

if __name__ == '__main__':
    print('------------------------------------------------------------')
    print('                  请使用管理员权限运行!!!')
    print('        说明:将PC中存在的无线网卡连接到同一个SSID')
    print('----------------------- >  IP设置  < -----------------------')
    print('(1).设置所有无线网卡为自动获取并连接')
    print('(2).设置所有无线网卡为静态IP并连接')
    print('(3).设置为静态IP并连接,设置一个IP通过ping来持续检测网卡是否断开')
    print('(4).不设置Ip只连接SSID')
    print('------------------------------------------------------------')
    choice = raw_input('输入您的选择\n')
    if choice == '1':
        test1(choice)
        print('------------------------------------------------------------')
        input('                     连接完成回车结束  ')
    elif choice == '2':
        test1(choice)
        print('------------------------------------------------------------')
        input('                     连接完成回车结束  ')
    elif choice == '3':
        test3(choice)
    elif choice == '4':
        test4()
        print('------------------------------------------------------------')
        input('                     连接完成回车结束  ')
    else:
        pass