11

I would like to read MAC address of attached device at the other side of the wire. Suppose to have 2 device, that are directly connected by wire. The first one (DeviceX) has Ethernet interface already configured (Static IP ....). The second one (DeviceY) doesn't know anything about DeviceX , but they are physically connected.

There are some way to read DeviceX MAC Address from DecviceY ? It's possible to send some particular packet from DeviceY in such a way the DeviceX reply with some packet ?

I have free access to network API for DeviceY, but about DeviceX i can't do anything.

Thanks in advance.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
arthur86
  • 531
  • 1
  • 3
  • 20
  • Is DeviceX running an operating system? Which one? – unwind Jun 14 '13 at 06:55
  • what do you mean by directly connected? Is it a crossed Ethernet cable between the 2 devices? Or is the connection through a hub/switch/router? – eyalm Jun 14 '13 at 07:03
  • unwind: DeviceX is a embedded system, with microcontroller using LwIP stack. – arthur86 Jun 14 '13 at 07:42
  • eyalm: I'm evaluating two possibilities. First one using crossed Ethernet cable. Second one through Ethernet hub. Do you think is better first or second one ? – arthur86 Jun 14 '13 at 07:45

2 Answers2

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

hazzelnuttie
  • 1,403
  • 1
  • 12
  • 22
  • Thank @Manimehalai I'm evaluating this option. Does the ARP protocol work if DeviceY doesn't know anything (No IP address, No Network configuration, No MAc address) about DeviceX ? The ARP protocol shuold be know the target IP Address to request or not necessary? – arthur86 Jun 14 '13 at 08:00
  • @hhazzelnuttie But the OP says he has no control over device X. He can't have it gratuitous ARP. Or did you mean device Y to issue the g-ARP? – Wes Miller Jan 09 '14 at 22:02
0

With the command GETMAC in a batch file :

@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

Or with WMIC in a batch file like this :

@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

Or with Batch and Windows PowerShell v4.0 : You can do something like this code :

@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

Or you can also do this :

@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
Hackoo
  • 18,337
  • 3
  • 40
  • 70