通过更改 IP 的最后八位字节使用 vbscript 更改默认网关
Change default gateway using vbscript by altering last octet of IP
要求用户手动输入 IP 即 192.168.0.2
然后网关将更改为 192.168.0.254
InStrRev() 和 Left() 函数应该可以工作,只是不能完全达到 运行。
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
使用 Split() 获取八位字节数组,并更改最后一个:
>> s = "192.168.0.2"
>> a = Split(s, ".")
>> a(3) = "254"
>> WScript.Echo Join(a, ".")
>>
192.168.0.254
看来我解决了自己的问题
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
我发现在将变量放入数组之前读取变量是关键
要求用户手动输入 IP 即 192.168.0.2 然后网关将更改为 192.168.0.254 InStrRev() 和 Left() 函数应该可以工作,只是不能完全达到 运行。
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
使用 Split() 获取八位字节数组,并更改最后一个:
>> s = "192.168.0.2"
>> a = Split(s, ".")
>> a(3) = "254"
>> WScript.Echo Join(a, ".")
>>
192.168.0.254
看来我解决了自己的问题
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
我发现在将变量放入数组之前读取变量是关键