VBS笔记, IP地址切换

本文介绍了一种使用VBS脚本实现网络适配器IP地址切换的方法,避免了频繁使用批处理带来的防火墙提示问题。通过设置不同的IP地址数组,脚本能够根据当前IP地址进行自动切换。

 

经常需要切换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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值