2014-11-24 144 views
1

我需要編寫一個小腳本我可以使用獲得只有一個特定IP地址的MAC地址。所以,你可以使用像VBScript:獲取指定IP地址的MAC地址(ONLY)

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 

不知何故,使用正則表達式或其它先進的分析方法,我想以削減下來呼應「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地址,我會想出一個方法,從本該分離。理想情況下,我可以運行一些其他實用程序(但無法找到),但它只是返回MAC。

回答

2
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實例獲取,讓它做的工作

+0

太棒了!謝謝。不幸的是,我完全不知道命令本身是如何工作的(這部分:'for/f「」skip = 3 tokens = 2「」%a in') – Beems 2014-11-25 16:11:56

+1

@Beems,意思是:執行'arp'命令,從其輸出,跳過三行,對於其餘行,使用空格作爲分隔符(默認情況下)獲取行中的第二個標記並回顯此標記 – 2014-11-25 18:12:53

0

比簡單的解析器多一點的代碼,但這種使用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 
+0

這似乎不適用於遠程地址。 – Beems 2014-11-25 16:29:46

+0

對不起。我不認爲你在尋找遠程地址 – Matt 2014-11-25 16:49:10