经常需要切换IP地址,原来用批处理,最近执行批处理,防火墙总有提示,麻烦。试试VBS吧。
如需修改网关
strGateway = Array("192.168.0.254")
strGatewayMetric = Array(1)
errGateways = netAdapterConfig.SetGateways(strGateway, strGatewaymetric)
********************************************************************
有一点疑问,Win32_NetworkAdapterConfiguration.IPAddress是数组,莫非一个Adapter可以有多个IP??
-----------------------------------------------------------------------------------------------------
strComputer = "."
Dim nIndexAdapter
strIPAddress150 = Array("150.245.179.22")
strIPAddress123 = Array("123.123.123.22")
strSubnetMask = Array("255.255.255.0")
'WScript.Echo "winmgmts:" & "{impersonationLevel=impersonate}!//" x & strComputer & "//root//cimv2"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!//" _
& strComputer & "/root/cimv2")
Set netAdapters = objWMIService.ExecQuery("SELECT * FROM " & "Win32_NetworkAdapter WHERE NetConnectionID = 'Local Area Connection'")
For Each netAdapter in netAdapters
WScript.Echo "Index:" & vbtab & CStr(netAdapter.Index)
nIndexAdapter = netAdapter.Index
Next
'
Set netAdapterConfigs = objWMIService.ExecQuery("SELECT * FROM " & "Win32_NetworkAdapterConfiguration WHERE Index = " & nIndexAdapter)
For Each netAdapterConfig in netAdapterConfigs
For Each strIpAdd in netAdapterConfig.IPAddress
Wscript.Echo "IPAddress: " & vbtab & strIpAdd
If strIpAdd = "150.245.179.22" Then
errEnable = netAdapterConfig.EnableStatic(strIPAddress123, strSubnetMask)
Else
errEnable = netAdapterConfig.EnableStatic(strIPAddress150, strSubnetMask)
End If
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
Next
本文介绍了一种使用VBS脚本实现网络适配器IP地址切换的方法,避免了频繁使用批处理带来的防火墙提示问题。通过设置不同的IP地址数组,脚本能够根据当前IP地址进行自动切换。
1038

被折叠的 条评论
为什么被折叠?



