获取IP地址并更改第3个八位位组

时间:2016-09-24 14:05:39

标签: vbscript

我使用以下内容获取IP地址(名为LAN2的NIC)

然后我需要更改第3个八位字节。以下是改变最后一个(到.15),但我不能将其修改为第3个 - 我该怎么做?

 Dim HostIPAddress : HostIPAddress = ""
Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim colAdapters : Set colAdapters = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Dim objAdapter
name = "LAN2"

Set wmi = GetObject("winmgmts://./root/cimv2")

deviceQry = "SELECT * FROM Win32_NetworkAdapter " & _
            "WHERE NetConnectionId = '" & name & "'"
For Each adapter In wmi.ExecQuery(deviceQry)
  addressQry = "SELECT * FROM Win32_NetworkAdapterConfiguration " & _
               "WHERE Index = " & adapter.DeviceId
  For Each config In wmi.ExecQuery(addressQry)
    If Not IsNull(config.IPAddress) Then
      HostIPAddress = Trim(config.IPAddress(0))
      Exit For
    End If
  Next
Next

strIP = HostIPAddress

wscript.echo HostIPAddress

i = InStrRev(strIP, ".")

strIP = Left(strIP, i) & "15"

1 个答案:

答案 0 :(得分:1)

在“。”上使用Split()要获取数组,请更改其第3个元素,使用Join()进行字符串化:

>> sIP = "1.2.3.4"
>> aIP = Split(sIP, ".")
>> WScript.Echo Join(aIP, "-")
>> aIP(2) = 254
>> WScript.Echo Join(aIP, ".")
>>
1-2-3-4
1.2.254.4
>>

(参见this