获取 IP 地址并更改第 3 个八位字节 [英] Obtain IP address and change 3rd Octet

查看:33
本文介绍了获取 IP 地址并更改第 3 个八位字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的来获取 IP 地址(称为 LAN2 的 NIC)

I am using the below to obtain an IP address (of NIC called LAN2)

然后我需要更改第三个八位字节.下面正在更改最后一个(到 .15),但我无法将其修改为第三个 - 我该怎么做?

I then need to change the 3rd octet. The below is changing the last (to .15) but I can't modify this to be the 3rd - how would I do this?

 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"

推荐答案

使用 Split() on "."要获取数组,请更改其第三个元素,使用 Join() 以字符串化:

Use Split() on "." to get an array, change its 3rd element, use Join() to stringify:

>> 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
>>

(参见这个)

这篇关于获取 IP 地址并更改第 3 个八位字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆