欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2024/4/14 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 用vbs自动切换不同网段的IP 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

开发和测试环境处于不同的网段,经常需要更改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

总结

以上是生活随笔为你收集整理的用vbs自动切换不同网段的IP的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。