针对windows操作系统的命令行网络地址切换脚本

    近来学习PYTHON觉得很有意思,用PYTHON语句来完成批处理实现的自动切换网络IP地址的功能,具体如下所示:

import wmi
import os
import time
c = wmi.WMI(namespace='root/CIMv2')
y = c.Win32_OperatingSystem()
for line in y:
    t = line.Caption
    print ('请输入数字1设置内网IP地址')
    print ('请输入数字2设置外网IP地址')
    inp = int(input('请输入数字:'))
    if (inp == 1):
        if t == 'Microsoft Windows XP Professional':
            os.chdir(r'c:\windows\system32')
            os.popen(r'netsh interface set interface name="本地连接" newname="bub"')
            time.sleep(5)
            os.popen(r'netsh interface ip set address name="bub" source=static addr=10.218.211.198 mask=255.255.255.0 gateway=10.218.211.254 gwmetric=auto')
            time.sleep(8)
            os.popen(r'netsh interface ip set dns name="bub" source=static addr=10.218.1.6 register=primary')
            time.sleep(5)
            os.popen(r'netsh interface ip set wins name="bub" source=static addr=none')
            time.sleep(5)
            print ('内网设置成功')
            break
        elif(t == 'Microsoft Windows 7 旗舰版'):
            os.chdir(r'c:\windows\system32')
            os.popen(r'netsh interface set interface name="本地连接" newname="bub"')
            time.sleep(5)
            os.popen(r'netsh interface ipv4 set address name="bub" source=static addr=10.218.211.198 mask=255.255.255.0 gateway=10.218.211.254 gwmetric=0')
            time.sleep(8)
            os.popen(r'netsh interface ipv4 set dns name="bub" source=static addr=10.218.1.6 register=primary')
            time.sleep(5)
            os.popen(r'netsh interface ipv4 set wins name="bub" source=static addr=none')
            time.sleep(5)
            print ('内网设置成功')
            break
        else:
            print ('操作系统不符合要求程序退出')
            sys.exit
    elif(inp == 2):
        if t == 'Microsoft Windows XP Professional':
            os.chdir(r'c:\windows\system32')
            os.popen(r'netsh interface set interface name="本地连接" newname="bub"')
            time.sleep(5)
            os.popen(r'netsh interface ip set address name="bub" source=static addr=10.219.66.27 mask=255.255.255.0 gateway=10.219.66.254 gwmetric=auto')
            time.sleep(8)
            os.popen(r'netsh interface ip set dns name="bub" source=static addr=61.128.114.133 both')
            time.sleep(5)
            os.popen(r'netsh interface ip set wins name="bub" source=static addr=none')
            time.sleep(5)
            print ('外网设置成功')
            break
        elif(t == 'Microsoft Windows 7 旗舰版'):
            os.chdir(r'c:\windows\system32')
            os.popen(r'netsh interface set interface name="本地连接" newname="bub"')
            time.sleep(5)
            os.popen(r'netsh interface ipv4 set address name="bub" source=static addr=10.219.66.27 mask=255.255.255.0 gateway=10.219.66.254 gwmetric=0')
            time.sleep(8)
            os.popen(r'netsh interface ipv4 set dns name="bub" source=static addr=61.128.114.133')
            time.sleep(5)
            os.popen(r'netsh interface ipv4 set wins name="bub" source=static addr=none')
            time.sleep(5)
            print ('外网设置成功')
            break
        else:
            print ('操作系统不符合要求程序退出')
            sys.exit
    else:
        print('不好意思,您的输入有误程序退出')
        break

    以上是主程序,程序的思路是通过OS的POPEN方法调用NETSH程序从而完成IP地址的配置,在使用WMI来读取操作系统的内部核心版本,通过对版本的判断使用不用的语句来实现不同计算机的IP地址的修改,我使用的PYTHON如下截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值