【问题标题】:VBScript: Get MAC address (ONLY) of specified IP AddressVBScript:获取指定 IP 地址的 MAC 地址(仅限)
【发布时间】:2014-11-24 21:56:33
【问题描述】:

我需要编写一个小脚本来获取特定 IP 地址的 MAC 地址。所以,你可以使用类似的东西

arp -a 192.168.x.x | FIND "192.168.x.x"

但您的结果仍将类似于:

Interface: 192.168.x.x --- 0x10
   192.168.x.x             00-00-00-00-00-00     dynamic  

不知何故,使用 REGEX 或其他高级解析方法,我想减少它以回显“00-00-00-00-00-00”。我可以使用以下内容,但我想要更干净的东西:

set obj_exec = obj_shell.Exec("%comspec% /C arp -a 192.168.x.x | FIND "192.168.x.x" > c:\temp\tmp_gatewayMAC.txt")
set obj_FSO = createobject("Scripting.FileSystemObject")
set obj_file = obj_FSO.opentextfile("c:\temp\tmp_gatewayMAC.txt", 1)
do until obj_file.AtEndOfStream
    str_line = obj_file.readline
    if instr(str_line, "dynamic") > 0 then
        str_MACaddress = str_line
    end if
loop

str_MACaddress = replace(str_MACaddress, "dynamic","")
str_MACaddress = replace(str_MACaddress, "192.168.x.x","")
str_MACaddress = replace(str_MACaddress, " ","")
str_MACaddress = trim(str_MACaddress)
vbscript.echo(str_MACaddress)

暂时忽略我们如何确定网关 IP 地址,因为我会为此提出一个独立的方法。理想情况下,我可以运行(但找不到)其他一些实用程序,例如 ARP,但它只返回 MAC。

【问题讨论】:

    标签: vbscript


    【解决方案1】:
    Option Explicit
    
    Dim IP
        IP = "192.168.1.1"
    
    Dim getMACCommand
        getMACCommand = """%comspec%"" /q /c ""for /f ""skip=3 tokens=2"" %a in ('arp -a "& IP &"') do echo %a"""
    
    Dim strMACAddress    
        strMACAddress = WScript.CreateObject("WScript.Shell").Exec(getMACCommand).StdOut.ReadAll
    
        WScript.Echo strMACAddress
    

    是的,可以使用正则表达式来完成,但由于数据是从生成的 cmd 实例中检索的,所以让它完成工作

    【讨论】:

    • 太棒了!谢谢你。不幸的是,我完全不知道命令本身是如何工作的(这部分:for /f ""skip=3 tokens=2"" %a in
    • @Beems,它的意思是:执行arp 命令,从它的输出中跳过三行,对于剩余的行,使用空格作为分隔符(默认情况下)获取行中的第二个标记并回显这个令牌
    【解决方案2】:

    比简单解析器多一点代码,但它使用 WMI 来查找具有特定 IP 地址的网络适配器的 mac 地址。由于它使用 WMI,因此 ip 地址将存储在一个数组中,我们需要检查该 ip 是否在该特定适配器的列表中。

    Option Explicit
    
    Dim objWMIService, colItems, objItem
    Dim strQuery, strIP, strMAC, strIPAddressToLocate
    
    strIPAddressToLocate = "169.244.119.133"
    strMAC = ""
    
    strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration"
    Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
    Set colItems      = objWMIService.ExecQuery( strQuery, "WQL", 48 )
    
    For Each objItem in colItems
        ' Ensure this adapter has at least one IP by checking if the value is an array. Filter out null/empty
        If IsArray( objItem.IPAddress ) Then
            If UBound( objItem.IPAddress ) = 0 Then
                ' Only one ip. Return that.
                strIP = objItem.IPAddress(0)
            Else
                ' Multiple IP's. Concat them so we can check contents with instr.
                strIP = Join( objItem.IPAddress, "," )
            End If
    
            If InStr(strIP,strIPAddressToLocate) <> 0 Then
                ' Get the mac address of the adapter matching this ip address. 
                strMAC = objItem.MacAddress
                ' Found the MAC we are looking for. Stop checking the loop
                Exit For
            End If
        End If
    Next
    
    If strMAC <> "" Then
        MsgBox "The IP Address: " & strIPAddressToLocate & " appears to be assigned to the adapter with the MAC Address: " & strMAC
    Else
        MsgBox "Unable to locate a network adapater with the Ip Address: " & strIPAddressToLocate
    End If
    

    【讨论】:

    • 这似乎不适用于远程地址。
    • 对不起。我不认为你在寻找远程地址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多