【问题标题】:Obtain IP and then change last octet获取 IP,然后更改最后一个八位字节
【发布时间】:2016-03-23 19:28:48
【问题描述】:

我需要通过 VBS 获取 PC 的 IP 地址,我正在使用以下方法:

strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"

Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
Set colItems      = objWMIService.ExecQuery( strQuery, "WQL", 48 )

For Each objItem In colItems
    If IsArray( objItem.IPAddress ) Then
        If UBound( objItem.IPAddress ) = 0 Then
            strIP = "IP Address: " & objItem.IPAddress(0)
        Else
            strIP = "IP Addresses: " & Join( objItem.IPAddress, "," )
        End If
    End If
Next

然后我正在使用以下内容来映射网络驱动器:

Option Explicit
Dim objNetwork 
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile

strDriveLetter = "Z:" 
strRemotePath = "\\10.121.34.140\c$" 
strUser = "user"
strPassword = "Password"
strProfile = "false"

Set objNetwork = WScript.CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword 

我想要做的是获取主机的IP(所以10.121.34.130)并将八位字节更改为.140并映射到那个。

我该怎么做?

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    InStrRev()Left() 函数应该是您所需要的。

    ' Sample starting address...
    strIP = "10.121.34.130"
    
    ' Get the index of the last period...
    i = InStrRev(strIP, ".")
    
    ' Append the new last octet starting there...
    strIP = Left(strIP, i) & "140"
    

    或者,作为单行:

    strIP = Left(strIP, InStrRev(strIP, ".")) & "140"
    

    或者,作为创建完整远程路径的单线:

    strIP = "\\" & Left(strIP, InStrRev(strIP, ".")) & "140\c$"
    

    【讨论】:

    • 完美 - 我将它调整为 strIP 是 MyIPAddress 并且效果很好 - 非常感谢!!
    猜你喜欢
    • 2014-05-13
    • 2011-11-27
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多