用vbs自动切换不同网段的IP

为解决开发和测试环境不同网段频繁更换IP的问题,本文介绍了一个实用的VBS脚本,通过运行该脚本可以快速实现网络配置的自动切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发和测试环境处于不同的网段,经常需要更改IP,手工修改非常麻烦。后来找到了一个vbs小脚本,实现了自动切换。

假如IP段分别为192.168.30.*和192.168.41.*

分别新添两个文件Turn30.vbs和Turn41.vbs ,内容如下:

strComputer = "." Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array("192.168.30.99") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.30.254") strDNS = Array("192.168.30.254") strGatewayMetric = Array(1) strDNS2 = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS=objNetAdapter.SetDNSServerSearchOrder(strDNS,strDNS2) If errEnable = 0 Then WScript.Echo "The IP address has been changed to "&strIPAddress(0) Else WScript.Echo "The IP address could not be changed." End If Next

strComputer = "." Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array("192.168.41.52") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.41.1") strDNS = Array("192.168.41.254") strGatewayMetric = Array(1) strDNS2 = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS=objNetAdapter.SetDNSServerSearchOrder(strDNS,strDNS2) If errEnable = 0 Then WScript.Echo "The IP address has been changed to "&strIPAddress(0) Else WScript.Echo "The IP address could not be changed." End If Next

使用方法:直接双击Turn30.vbs或Turn41.vbs即可执行。

参考网站:
http://www.microsoft.com/china/technet/community/scriptcenter/network/scrnet01.mspx

邀月注:本文版权由邀月和优快云共同所有,转载请注明出处。
助人等于自助! 3w@live.cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值