`
java-mans
  • 浏览: 11315899 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

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

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

view plaincopy to clipboardprint?
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")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
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.30.99")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.30.254")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
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

view plaincopy to clipboardprint?
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")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
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")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
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

助人等于自助!

======================

配置静态 IP 地址

更新日期: 2003年02月17日

描述

将计算机的 IP 地址设置为 192.168.1.141,并将 IP 网关设置为 192.168.1.100。

脚本代码

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

For online peer support, join the microsoft.public.windows.server.scripting community on the msnews.microsoft.com news server. To provide feedback or report bugs in sample scripts or the Scripting Guide, please contact Microsoft TechNet.

免责声明

以上示例脚本不会获得由任何Microsoft标准支持计划或服务项目所提供的支持。这些示例脚本在提交时并未附带任何形式的保证承诺。不仅如此,Microsoft公司还不加限定条件地针对所有默许保证责任加以进一步否认,这其中便包括出于特定目的而针对适销性或适用性所承担的默许保证责任。因使用或执行上述示例脚本及文档资料而导致的全部风险均由读者自行承担。在任何情况下,Microsoft公司及其创作人员、亦或与上述脚本的创意、编制及提交有关的任何人员均无须针对因使用或无法使用上述示例脚本或文档资料所导致的任何损害(其中包括,企业利润损失、经营中断、业务信息丢失及其它经济损失)承担责任;即使Microsoft公司已被告知造成这种损害可能性,上述免责条款依然适用。

======================


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/downmoon/archive/2009/12/22/5054744.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics