0

I've a problem getting Mac address using Java. If a machine has VM installed i get different MAC address. Same happens when i try to filter ip address to get its mac while pinging (checking reachabliltiy) of my private network ip address i.e 172.xx.xx.xx

Below is a piece of code i'm trying

public static void main(String args[]) throws IOException {
    String loca= System.getProperty("user.home")+File.separatorChar+"Documents"+File.separatorChar+"mac.txt";
    FileWriter fw = new FileWriter(loca);
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface interf = interfaces.nextElement();
        if (!interf.isLoopback()){ //interf.isUp() && !interf.isLoopback()
         Enumeration<InetAddress> mac2 = interf.getInetAddresses();
         while(mac2.hasMoreElements()){
            String indivIP= mac2.nextElement().getHostAddress();
            if(indivIP.contains(".")){
                try {
                    String iptest = "172.x.x.x.x."; //my private network ip
                    String test2= InetAddress.getLocalHost().getHostName();
                    System.out.println("HOST NAME = "+test2);
                    boolean pingStatus = InetAddress.getByName(indivIP).isReachable(3000);
                    if(pingStatus == true){
                        System.out.println("IP address = " + indivIP);
                         byte[] mac = interf.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) ? "" : ""));        
                            }
                           System.out.println("mac address to use = "+sb.toString().toLowerCase());
                           String macAddress = sb.toString().toLowerCase();
                           try {
                                fw.append("\n");
                                fw.append("IP        : " + indivIP);
                                fw.append("\n");
                                fw.append("MacAddress: " + macAddress);
                                fw.append("\n");
                                fw.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                    }
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                //future upgrade
            }
         }
     }
   }
fw.flush();
fw.close();
}

Other subjects that I've been looking at Get MAC address on local machine with Java

Get MAC Address of System in Java

All I want is to get Ethernet Adapter network.. my physical mac address that comes with hardware nothing more or less.

Below is a sample of my output of ipconfig /all in cmd

C:\***********>ipconfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : ***********
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter Local Area Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : *********** Client Virtual Adapter
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Local Area Connection* 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft Hosted Network Virtual Adapter
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Local Area Connection* 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
   Physical Address. . . . . . . . . : ((((((((((((((((((( MY REAL MAC ADDRESS)))))))))))))))))))
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : BCM*********** Wireless Network Adapter
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : ***********
   IPv4 Address. . . . . . . . . . . : ***********(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : ***********
   Lease Expires . . . . . . . . . . : ***********
   Default Gateway . . . . . . . . . : ***********
   DHCP Server . . . . . . . . . . . : ***********
   DHCPv6 IAID . . . . . . . . . . . : ***********
   DHCPv6 Client DUID. . . . . . . . : ***********

   DNS Servers . . . . . . . . . . . : ***********
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : ***********
   Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
   Physical Address. . . . . . . . . : (((  REAL MAC ADDRESS)))
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes

Ethernet adapter VirtualBox Host-Only Network:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : ***********(Preferred)
   Autoconfiguration IPv4 Address. . : ***********.77(Preferred)
   Subnet Mask . . . . . . . . . . . : ***********
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : ***********
   DHCPv6 Client DUID. . . . . . . . : ***********

   DNS Servers . . . . . . . . . . . : ***********
                                       ***********
                                       ***********
   NetBIOS over Tcpip. . . . . . . . : Disabled

Ethernet adapter VMware Network Adapter VMnet1:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet
1
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : ***********
   IPv4 Address. . . . . . . . . . . : ***********(Preferred)
   Subnet Mask . . . . . . . . . . . :***********
   Default Gateway . . . . . . . . . :
   DHCP Server . . . . . . . . . . . : ***********
   DHCPv6 IAID . . . . . . . . . . . : ***********
   DHCPv6 Client DUID. . . . . . . . : ***********

   DNS Servers . . . . . . . . . . . : ***********
                                       ***********
                                       ***********
   NetBIOS over Tcpip. . . . . . . . : Disabled

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet
8
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : ***********
   IPv4 Address. . . . . . . . . . . : ***********(Preferred)
   Subnet Mask . . . . . . . . . . . : ***********
   Lease Obtained. . . . . . . . . . : ***********
   Lease Expires . . . . . . . . . . : ***********
   Default Gateway . . . . . . . . . :
   DHCP Server . . . . . . . . . . . : ***********
   DHCPv6 IAID . . . . . . . . . . . : ***********
   DHCPv6 Client DUID. . . . . . . . : ***********

   DNS Servers . . . . . . . . . . . : ***********
                                       ***********
                                       ***********
   NetBIOS over Tcpip. . . . . . . . : Disabled

Tunnel adapter isatap.{***********}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft *********** Adapter
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.{***********}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft *********** Adapter #2
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter Local Area Connection* 14:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : ***********Interface
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.{***********}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft *********** Adapter #4
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter isatap.{***********}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Microsoft *********** Adapter #7
   Physical Address. . . . . . . . . : ***********
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
Community
  • 1
  • 1
Theg8nj
  • 135
  • 1
  • 16

1 Answers1

3

Are you running your Java code inside a virtual machine? Virtual machines have virtual network adapters, so you will not see your physical MAC inside your virtual machine (unless you manage to pass the actual device from your host to your VM).

Edit 1: I modified your example. With the classes you use the only way I could think of is to use the adapter names. I have some adapters that originate from a VM. They all have either VirtualBox or Hyper-V in their name. You would need to extend VIRTUAL_ADAPTER_NAMES to match your VMWare or whatever names.

public class Mac {
    private static final int TIMEOUT = 3000;
    private static final String[] VIRTUAL_ADAPTER_NAMES = { "VirtualBox", "Hyper-V" };

    private static boolean isRelevant(NetworkInterface networkInterface) throws IOException {
        if (!networkInterface.isUp()) {
            return false;
        }
        if (networkInterface.isVirtual()) {
            return false;
        }
        if (networkInterface.isLoopback()) {
            return false;
        }
        String name = networkInterface.getDisplayName();
        for (String vName : VIRTUAL_ADAPTER_NAMES) {
            if (name.contains(vName)) {
                return false;
            }
        }
        return true;
    }

    private static boolean isIpv4Address(String address) {
        return address.matches("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
    }

    private static void printInfoForInterface(NetworkInterface networkInterface) throws IOException {
        System.out
                .println("Interface: \t" + networkInterface.getDisplayName() + " (" + networkInterface.getName() + ")");

        Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            String address = addresses.nextElement().getHostAddress();
            if (isIpv4Address(address)) {
                if (InetAddress.getByName(address).isReachable(TIMEOUT)) {
                    System.out.println("IP address: \t" + address);

                    printMac(networkInterface.getHardwareAddress());
                }
            }
        }
    }

    private static void printMac(byte[] mac) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""));
        }
        System.out.println("MAC: \t\t" + sb.toString().toLowerCase());
    }

    public static void main(String args[]) throws IOException {
        System.out.println("HostName: \t" + InetAddress.getLocalHost().getHostName());
        System.out.println("------------------------------------------\n");

        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            try {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (isRelevant(networkInterface)) {
                    printInfoForInterface(networkInterface);
                }
            } catch (IOException e) {
                System.err.println("An error occured.");
                e.printStackTrace(System.err);
            }
        }
    }
}

The output I get (with plenty of virtual adapters) is

HostName:    HOST
------------------------------------------

Interface:   Intel(R) Dual Band Wireless-AC 7260 (wlan1)
IP address:  192.168.2.105
MAC:         xxxxxxxxxxxx
cmoetzing
  • 742
  • 3
  • 16
  • Nope i'm not running Java code inside VM. However, running Java through Eclipse on my pc where I've VMware workstation and virtual box are installed. – Theg8nj Mar 02 '17 at 11:57
  • finally got mac adress trying this one .. tried tons of answers no one gave me mac address – Arif Nouman Khan Dec 14 '17 at 16:55