Search code examples
javaipmac-address

How to get remote machines mac addresses java


Possible Duplicate:
Query ARP cache to get MAC ID

I need to get the mac addresses of all the remote machines connected to my home network. i can read the mac address of my own machine using this code below but i don't know how to get the mac addresses of other machines on my network. or is it possible or not.

import java.net.InetAddress;
import java.net.NetworkInterface;
class Test {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost();

NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();

for (int i = 0; i < mac.length; i++) {
  System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
}
}  

output (MyComputer's 6byte mac address)
30-60-77-12-cd-99


Solution

  • How to get remote machines mac addresses

    You cannot do that - not in the general case. MAC addresses are only available for machines in the same physical network segment. Those machines communicate directly with each other using the ARP protocol to "translate" IP addresses to physical (MAC) addresses.

    As soon as an IP router or any other Layer-3 device is crossed, any MAC information is generally lost.

    Bottom line: you cannot get the MAC address of a remote node over the Internet - you cannot even do that in any moderately sized private network, as implemented in e.g. most medium to large companies.