python一键配置多个IP

python 专栏收录该内容
1 篇文章 0 订阅

工作需要,要在电脑上配置多个虚拟网卡,每个网卡上可能要配置多个不同的IP,也会时常对虚拟网卡配置的IP进行大范围的修改。比较麻烦,修改一次要花费不少时间,而且还总是容易少配置一些IP,导致一些程序无法启动,所以动不动就会把心态搞得稀碎。于是想了个办法,用自己不成熟的python技术做了个脚本,解决了上面的问题。因为也算是python的初学者,大部分东西都是需要用哪一块了,就去搜索哪一块的知识,现学现卖。所以可能对于一些大佬来说,我做的东西十分不入眼,这也是没办法的事,不过我是欢迎大佬们能对我写的东西提一些缺点问题啊或者是意见什么的,能够促进我不断进步。费话不多说,亮代码。

# 一键修改IP工具,但是有些电脑可能不太好用,不知道是为什么
import os
import sys
import wmi
import ctypes
import subprocess
# from __future__ import print_function

def isAdmin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def getWinNetList(net_list):
    netcardnum = 0
    net_card = {} 
    wmiservice = wmi.WMI()
    configs = wmiservice.Win32_NetworkAdapterConfiguration(IPEnabled=True)  # 获取到本地所有有网卡信息,list
    for nic in configs:
        if nic.MACAddress is not None:
            net_card['num'] = netcardnum
            net_card['mac'] = nic.MACAddress
            # net_card['ip'] = []
            # if nic.IPAddress is not None:
            #     for i in range(len(nic.IPAddress)):
            #         net_card['ip'].append(nic.IPAddress[i])
                    # print('ip:'+ str(nic.IPAddress[i]))
            net_list.append(net_card)
            netcardnum += 1
        net_card = {} 
    return configs

         
def setIpMask(adapter, iplist, masklist): 
    for i in range(len(iplist)):
        print('ip:' + str(iplist[i]) + ' mask:' + str(masklist[i])) 
    # 开始执行修改ip、子网掩码、网关
    ipres = adapter.EnableStatic(IPAddress=iplist, SubnetMask=masklist)
    if ipres[0] == 0:
        print('设置IP成功')
    else:
        if ipres[0] == 1:
            print('设置IP成功')
        else:
            print('修改IP失败')
            print(ipres[0])
            return False
    # #修改网关
    # wayres = adapter.SetGateways(DefaultIPGateway=interway, GatewayCostMetric=[1])
    # if wayres[0] == 0:
    #     print('设置网关成功')
    # else:
    #     print('修改网关失败')
    #     return False
    # #修改dns
    # dnsres = adapter.SetDNSServerSearchOrder(DNSServerSearchOrder=dns)
    # if dnsres[0] == 0:
    #     print('设置DNS成功,等待3秒刷新缓存')
    #     sleep(3)
    #     # 刷新DNS缓存使DNS生效
    #     os.system('ipconfig /flushdns')
    # else:
    #     print('修改DNS失败')
    #     return False



# 执行windows命令
def execCommand(commands) -> list:
    """执行windows命令"""
    if not commands:
        return list()
    # 子进程的标准输出设置为管道对象
    if isinstance(commands, str):
        commands = [commands]
    return_list = []
    for i in commands:
        p = subprocess.Popen(i, shell=True, stdout=subprocess.PIPE, universal_newlines=True)
        # p.wait()
        res = "".join(p.stdout.readlines())
        return_list.append(res)
    return return_list


def getConfigurableNetCard():
    net_card_data = []
    res = execCommand("ipconfig /all")
    temp_dict = {} 
    for x in res[0].splitlines():
        # 如果发现新的适配器,则重置上一个网卡是否可用的状态
        if "网适配器" in x:
            if "以太网适配器" in x:
                if '蓝牙' in x:
                    continue
                else:
                    temp_dict['card_name'] = x.split(" ",  1)[1][:-1]
                    temp_dict['mac'] = []
                    # print("当前网卡 %s" % (temp_dict['card_name']))
                    continue

        if temp_dict:
            # # 匹配IP正则
            # if "IPv4 地址" in x:
            #     pattern = re.compile(r'((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}')
            #     ip = pattern.search(x).group()
            #     temp_dict['ip'].append(ip)
            #     continue 
            if "物理地址" in x:
                mac = x.split(': ')[1].replace('-', ':')
                temp_dict['mac'].append(mac)
                net_card_data.append(temp_dict)
                temp_dict = {}
            else:
                continue

    return net_card_data 


def getModifyNetCardMac(netCardName ,configuraList):
    for i in range(len(configuraList)):
        if netCardName in configuraList[i]['card_name']:
            mac = configuraList[i]['mac'][0]
            break
    return mac


def getModifyNetCardNum(netCardMac, winNetLists):
    for i in range(len(winNetLists)):
        if netCardMac in winNetLists[i]['mac']:
            num = winNetLists[i]['num']
            break
    return num


def getNetCardName(str):
    name = ''
    if 'netcardname' in str:
        name =  str.split(':')[1]
    return name


def getConfigIpMask(str):
    info = {}
    if 'ip' in str:
        ip = str.split('ip:')[1].split(' ')[0]
        if 'mask' in str:
            mask = str.split('mask:')[1].split(']')[0]
            info['ip'] = ip
            info['mask'] = mask
    return info


def ReadConfig(netcardname):
    data = {}
    data['ip'] = []
    data['mask'] = []
    linenum = 0
    config = 'config.txt'
    if True == os.path.exists(config):
        with open(config,'r') as f: 
            for line in f.readlines():
                line = line.strip('\n')  #去掉列表中每一个元素的换行符
                if 0 == linenum:
                    tmpName = getNetCardName(line)
                    netcardname.append(tmpName)
                    print('要配置的网卡:' + str(netcardname))
                else:
                    tmpinfo = getConfigIpMask(line)
                    data['ip'].append(tmpinfo['ip'])
                    data['mask'].append(tmpinfo['mask'])
                linenum += 1
    # print(data)
    return data

 
def main():
    netcardname = []
    winNetLists = [] 

    datainfo = ReadConfig(netcardname)

    configuraList = getConfigurableNetCard()

    configs = getWinNetList(winNetLists)

    modifyMac = getModifyNetCardMac(netcardname[0], configuraList)

    modifyNetCardNum = getModifyNetCardNum(modifyMac, winNetLists)

    setIpMask(configs[modifyNetCardNum], datainfo['ip'], datainfo['mask'])

    input('按回车退出')



if __name__ == '__main__':
    
    if isAdmin():
        print('管理员权限操作')
        main()
    else:
        if sys.version_info[0] == 3:
            ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
        else:#in python2.x
            ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)

关于config.txt文件的配置要求如下:

第一行必须配置网卡名,
且只可以改“netcardname:”后面的字符
其它几行配置IP和掩码以及注释,须按照
XXXX[ip:xxx.xxx.xxx.xxx mask:xxx.xxx.xxx.xxx]的格式
其中x需要填数字,X可选填汉字也可不填,
注意ip的最后一位与mask字符中间有一个空格.

例如:config.txt

netcardname:ElecOC
设备1[ip:10.16.94.2 mask:255.0.0.0]
设备2[ip:10.16.94.6 mask:255.0.0.0]
设备3[ip:10.24.10.2 mask:255.0.0.0]
设备4[ip:10.24.94.6 mask:255.0.0.0]
设备5[ip:10.24.94.2 mask:255.0.0.0]
设备6[ip:10.24.10.6 mask:255.0.0.0]
设备7[ip:10.28.94.6 mask:255.0.0.0]
设备8[ip:10.28.94.2 mask:255.0.0.0]

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值