Nicolas
Nicolas

Reputation: 331

Request MAC address from Raspberry Pi

I have a Linux machine that is connected directly to a Raspberry Pi via an Ethernet cable. Is there a way to programmatically in C get the Raspberry Pi's MAC address based on which Ethernet port on my computer it is connected to (eth0, eth1 etc...)

For instance, say I have two Raspberry Pi's, one connected to eth0 and another to eth1. I would like to get the MAC address of only the Pi that is connected to eth0. How would I do that?

Upvotes: 2

Views: 786

Answers (1)

fluter
fluter

Reputation: 13786

Depending on do you know the ip address of the Pi, there are two cases:

  • The Pi has IP address and are known to your program, you can just send any data to it, e.g. an ICMP PING packet, the networking stack will send out ARP requests for the address, and when the Pi respond with its MAC address, you can get it from the ARP table, which can be viewed by the command arp -n.

  • The Pi's IP is not known, then you send a broadcast packet through the connected interface, eth0 in this case, for example, ping -b -I eth0 255.255.255.255, the Pi will also respond with its MAC address, and you can get it from the local arp table.

Now for how to do this in programmatic way, you can send the packet using raw sockets, and read arp table through parsing the proc entry /proc/net/arp.

Upvotes: 2

Related Questions