-5

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.

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • Have you tried this answer http://stackoverflow.com/questions/6164167/get-mac-address-on-local-machine-with-java ? – cyon Sep 24 '13 at 11:17
  • @cyon I loked through it but couldnt understand it properly, i am a newbie; i think maybe i should use NetworkInterface.getNetworkInterfaces() but i searched and in through this i can get ip but not mac of ethernet – Daksh Shah Sep 24 '13 at 11:23
  • @cyon Sir the question is updated. – Daksh Shah Sep 28 '13 at 20:14

1 Answers1

3

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • @DakshShah - The question is tagged with OSX, and my answer is for OSX. If you actually are using Windows, the command is called "ipconfig" I believe. My answer explains one approach for doing this in Java, but I can't guarantee it will work ... for the reasons stated. – Stephen C Sep 24 '13 at 11:33
  • I have nothing more to add to my answer. I think that the last paragraph of my answer sums up the situation as best as is possible. You can stop someone spoofing their IP address, and you can't portably or reliably detect when they have. Full stop. Asking people to repeatedly "have another look" won't change the answer. – Stephen C Sep 28 '13 at 23:36