Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding MacAddress from IP Address in a platform independent way

I need to findout the mac address of the device from which my device gets TCP requests, I ll be getting the ip address of the device by tcp endpoint but i need to find out the mac address of the device.My application will be running on both windows and linux, so please suggest me a cross platform method to find the mac address.. Any boost libraries will help me doing the same??

like image 997
Achuthananda MP Avatar asked Feb 07 '12 07:02

Achuthananda MP


2 Answers

Firstly, you can't find the MAC address for any network interface that is not on the same local area network. That information is not transmitted beyond the router.

There is a command line tool called arp that is available on Unix and also Windows that will list IP addresses and MAC addresses of interfaces that have been in communication with your PC. i.e.

arp -a

on Windows gives something like:

Interface: 9.175.198.236 --- 0x2
  Internet Address      Physical Address      Type
  9.175.198.129         00-1b-53-46-fa-7f     dynamic

and on a Unix-alike looks like:

foo.bar.com (10.27.68.72) at 00:50:56:AE:00:0B [ether] on eth0
baz.bar.com (10.27.68.77) at 00:50:56:AE:00:10 [ether] on eth0
? (10.27.68.1) at 00:50:5A:1B:44:01 [ether] on eth0

You can try invoking it and parsing the output programmatically.

like image 150
JeremyP Avatar answered Oct 09 '22 13:10

JeremyP


arp source code is available in the below link, take the piece of code that interests you! It is c code so it should work fine.

http://www.opensource.apple.com/source/network_cmds/network_cmds-328/arp.tproj/arp.c

like image 32
Vishvesh Avatar answered Oct 09 '22 13:10

Vishvesh