0

I am trying to develop an open-source network monitoring application The application scan the network to find all connected devices. It shows all IPs in the given range but the connected devices will appear with checked box (JCheckBox). I list the connected devices in a JTable with information about each device such as name or IP address using address.getHostName() and address.getHostAddress() -Methods in the class InetAddress- Now I am trying to get the physical address (MAC). I tried this code but the application shows the local device only; the devices (connected or disconnected) in the given IPs range did not appear.

byte[] mac = NetworkInterface.getByInetAddress(address).getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
String MAC = sb.toString();
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • Duplicate of https://stackoverflow.com/questions/1238963/query-arp-cache-to-get-mac-id (and numerous others). – Pixel Dec 25 '17 at 18:47

1 Answers1

1

To request MAC (Hardware) address you need to make ARP lookup.

Answer to appropriate question may help you. There are several ways defined there:

  • arp -a lookup (UNIX-specific way)
  • SNMP request
  • Send your own ARP request packet

Anyway you can get MAC address that belongs only to devices within your local network.

Community
  • 1
  • 1
Dmitry Poroh
  • 3,705
  • 20
  • 34