本文介绍了通过更改IP的最后一个八位字节,使用vbscript更改默认网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要求用户手动输入IP,即192.168.0.2然后,网关将更改为192.168.0.254InStrRev()和Left()函数应该可以正常运行.
User is asked to manually input the IP i.e 192.168.0.2The gateway will then change to 192.168.0.254The InStrRev() and Left() functions should work just can't quite get it to run.
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"
Set colNetAdapters = objWMIService.ExecQuery _
(strQuery)
strIPAddress = Array(InputBox("IP address"))
strSubnetMask = Array("255.255.255.0")
strGateway = Left(strIPAddress, InStrRev(strIPAddress, ".")) & "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."
Else
WScript.Echo "The IP address could not be changed."
End If
next
推荐答案
好像我解决了自己的问题
Looks like I solved my own problem
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"
Set colNetAdapters = objWMIService.ExecQuery _
(strQuery)
strIPAddress = (InputBox("IP address"))
strSubnetMask = Array("255.255.255.0")
strGateway = Left(strIPAddress, InStrRev(strIPAddress, ".")) & "254"
strIPAddress = Array(strIPAddress)
strGateway = Array(strGateway)
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
我发现在将变量放入数组之前阅读变量是关键
I found reading the variables before putting them into an array was the key
这篇关于通过更改IP的最后一个八位字节,使用vbscript更改默认网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!