LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I find the mac address for a given ip address? (https://www.linuxquestions.org/questions/programming-9/how-do-i-find-the-mac-address-for-a-given-ip-address-789537/)

bostan 02-16-2010 01:24 PM

How do I find the mac address for a given ip address?
 
Hi All,
I am trying to find out the mac address of the eth0 port on linux probe. This port is controlled by kernal and hence I don't have control over it. How do I find out the mac address of this port ? Is there any system call which will take the ip address and will do the lookup on the linux's ip table to get the mac address ?
-Thanks

catkin 02-16-2010 01:39 PM

From the command line, ifconfig provides the information you want and its output could be parsed by several languages.

irmin 02-16-2010 01:42 PM

Hi,

you can access the kernel ARP tables with the SIOCSARP, SIOCDARP and SIOCGARP ioctls on any AF_INET socket. See arp(7) for details.

There is also a command line tool called "arp" that can modify and view the ARP tables.

irmin 02-16-2010 01:47 PM

Maybe also the ioctls SIOCGIFHWADDR, SIOCSIFHWADDR described in netdevice(7) are of interest.

bostan 02-16-2010 08:11 PM

How do I find the mac address for a given ip address?
 
Hi ,
Thanks for the info. I tried using ioctl call and when I try to get the arp entry by calling ioctl(s,SIOCGARP,&a) I get the error saying protocol not supported. I am using fedora 12 linux 2,6.22
Am I missing something here ?

irmin 02-17-2010 04:04 AM

2 Attachment(s)
The SIOCGARP ioctl call expects several parameters already set in the request structure. I put two small programs in the appendix: The first one will query the ARP tables for the hardware address belonging to an IP address (this does not work on your own addresses, only on devices, that are on the same subnet). The other one will query for the hwaddress of a local interface.

I hope they will help you with your problem.

bostan 02-17-2010 09:28 AM

Thanks Irmin
I can see the arp entry now for a given IP address.
-Thanks a lot and God bless you :)


All times are GMT -5. The time now is 09:16 PM.