Question

I am using the following code to get the mac adresses: (in main)

Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

    while (interfaces.hasMoreElements())
    {
      NetworkInterface nif = interfaces.nextElement();
      byte[] lBytes = nif.getHardwareAddress();
      StringBuffer lStringBuffer = new StringBuffer();

      if (lBytes != null)
      {
        for (byte b : lBytes)
        {
          lStringBuffer.append(String.format("%1$02X ", new Byte(b)));
        }
      }

      System.out.println(lStringBuffer);
    }

I have included the following headers:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;

If I run the code, I get the output very long (mostly blank) and I also get a list of 6 addresses in between I checked my Ethernet's mac address through ip config, and it is in the list;

I want to know that which one of them is Ethernet's mac even if I run it on different PCs and I also wanted to know, how to check if they have spoofed the mac address or not...

By Spoofed I mean that if someone after installing windows, changes the mac address in registry or by a software. I read it in one of the posts on some other question in stack overflow, one person commented that the code he wrote would work even if someone had spoofed the mac and therefore the code isn't good. So I suppose that it can be done in java. I would have asked there but I couldn't. I meant that if the PC or laptop doesn't have a wireless, it should take the Ethernet mac address.

Was it helpful?

Solution

In a certain sense, the MAC address of a network interface that is turned off does nor exist. Consider that the MAC address may be supplied to the interface as a command line parameter when the device is initialized. At any rate, this one of the reasons why Java would have difficulty reporting a MAC address on an inactive network interface.

In practice, you may be able to use Runtime.exec(...) or similar to run the ifconfig command, and then parse the output to extract the MAC address. However, I don't know if this will always work.


I don't think what you are trying to do makes much sense (for the reason above), but if you are going to try this, you first need to confirm that running ifconfig tells you the MAC address for a WiFi network interface that is not connected and/or not powered on. The manual page is here. Once you've confirmed that, read the javadocs for Runtime.exec(...) and ProcessBuilder. If you need a more basic tutorial, try this one: http://alvinalexander.com/java/java-exec-processbuilder-process-1


I want to know that which one of them is ethernet's mac even if i run it on different PCs

You can't reliably distinguish an ethernet from a WiFi network based on the MAC address.

I Also wanted to know that how to check if they have spoofed the mac adress or not

By spoofed, I assume that you want to know if the MAC address of the NIC has been changed from its (hardware) default. You cannot tell that from Java. Indeed, I doubt you can even tell that natively.

Frankly, it is doubtful that you are going to get anywhere if you try to do this in (pure) Java. The standard Java APIs simply don't support this kind of thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top