10

我想读取电线另一端连接设备的 MAC 地址。假设有 2 个设备,它们直接通过电线连接。第一个(DeviceX)已经配置了以太网接口(静态IP ....)。第二个(DeviceY)对 DeviceX 一无所知,但它们是物理连接的。

有什么方法可以从 DecviceY 读取 DeviceX MAC 地址?是否可以从 DeviceY 发送一些特定的数据包,这样 DeviceX 会回复一些数据包?

我可以免费访问 DeviceY 的网络 API,但对于 DeviceX,我无能为力。

提前致谢。

4

2 回答 2

9

Computers connected to the same TCP/IP local network can determine each other's MAC addresses. The technology called ARP - Address Resolution Protocol included with TCP/IP makes it possible.

From Windows terminal "arp -a" gives the list of ARP entries

For more on ARP go to this link

____EDIT_____

@arthur86 This can be done by sending "Gratuitious ARP" from Device X (broadcast). A gratuitous ARP request is an AddressResolutionProtocol request packet where the source and destination IP are both set to the IP of the machine issuing the packet and the destination MAC is the broadcast address ff:ff:ff:ff:ff:ff.

Device Y will have its arp cache updated with Device X MAC. Using the arp cache entries, Device Y can get the IP and MAC of Device X.

Check this link for details about Gratuitious ARP

于 2013-06-14T07:05:34.417 回答
0

使用GETMAC批处理文件中的命令:

@echo off 
Title With GETMAC Command
@For /f "tokens=1 delims=," %%a in ('getmac /NH /FO csv ^| find /I "N/A"') do  (
    Set "MAC=%%~a"
)
echo MAC Address = %MAC%
pause

或者WMIC在这样的批处理文件中:

@echo off 
Title with WMIC
@for /f "delims=" %%M in (
'Wmic Nicconfig Where IPEnabled^=true list full ^| find /I "MACAddress"'
) do ( 
    Set "%%M"
)
echo MAC Address = %MACAddress%
pause

或者使用 Batch 和 Windows PowerShell v4.0:您可以执行以下代码:

@echo off 
Title Get-NetAdapter Physical 
@for /f "tokens=2 delims=Up" %%a in ('Powershell -C "Get-NetAdapter -Physical" ^| findstr /I "Up"') do (
    @for /f "tokens=2 delims= " %%b in ('echo "%%a"') do (
        Set "PhysicalAddress=%%b"
    )
)
echo MAC Address = %PhysicalAddress% 
pause

或者你也可以这样做:

@echo off
Title GET Interface Description and MAC ADDRESS NET With Get-NetAdapter in Powershell And Batch
Set psCmdInterface="&{Get-NetAdapter -Physical | Where-Object { $_.Status -eq 'Up' } | Select-Object -Property InterfaceDescription}"
Set psCmdMAC="&{Get-NetAdapter -Physical | Where-Object { $_.Status -eq 'Up' } | Select-Object -Property MacAddress}"
REM Powershell -C %psCmdInterface%
for /f "Skip=3 tokens=1 delims=" %%a in ('Powershell -C %psCmdInterface%') do Set "Interface=%%a"
REM Powershell -C %psCmdMAC%
@for /f "Skip=3 tokens=1 delims= " %%a in ('Powershell -C %psCmdMAC%') do Set "MAC=%%a"
echo Interface = "%Interface%"
echo MAC ADDRESS = %MAC%
pause
Exit
于 2020-08-06T11:43:53.660 回答