简体   繁体   中英

Easiest way to get MAC address from a (non-local) IP address in IPv4

Is there an easy way to get a MAC address of a machine connected to my app via a TCP/IPv4 socket?

Or in more general terms:

If I have the IP address, what is the best way to get the corresponding MAC address in IPv4?

MAC addresses are used only for the next hop. This basically means the client's original MAC address won't be visible after it's first hop. In the general sense, if you are not on the same physical segment as the client, you have NO way of taking an IP and getting the MAC (or vice/versa).

You can't, without having something (or the client itself) on that other network.

MAC addresses are used on the second layer, the data-link layer, of the OSI model for networking . MACs are specific to Ethernet. When you get to the Internet, IP addresses are used. Not everything connected to the Internet uses Ethernet at lower layers.

There are also other protocols other than IP that can be used.

Basically, your computer only knows what is on its physical network segment. If it has to go outside for anything else, it only knows the MAC of the gateway to get there.

It is the ARP table's job to keep that information. The best thing to do would be to read it out of there.

You might consider reading the answers to Query ARP cache to get MAC ID . The questioner was using Java, but otherwise its your identical question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM