在java中获取物理mac地址

时间:2017-02-28 08:49:53

标签: java windows virtual-machine exe

我在使用Java获取Mac地址时遇到问题。如果机器安装了VM,则会获得不同的MAC地址。当我尝试在我的专用网络IP地址(即172.xx.xx.xx)的ping(检查重新连接)时尝试过滤ip地址以获取其mac时,也会发生同样的情况。

下面是我正在尝试的一段代码

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();
}

我一直在关注的其他主题 Get MAC address on local machine with Java

Get MAC Address of System in Java

我想要的只是获得以太网适配器网络..我的物理mac地址随硬件附带或多或少。

以下是我在cmd

中输出ipconfig / all的示例
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

1 个答案:

答案 0 :(得分:1)

您是否在虚拟机中运行Java代码?虚拟机具有虚拟网络适配器,因此您无法在虚拟机中看到物理MAC(除非您设法将实际设备从主机传递到VM)。

编辑1: 我修改了你的例子。使用类,我使用的唯一方法是使用适配器名称。我有一些来自VM的适配器。他们的名字中都有 VirtualBox Hyper-V 。您需要扩展VIRTUAL_ADAPTER_NAMES以匹配您的VMWare或任何名称。

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);
            }
        }
    }
}

我得到的输出(有很多虚拟适配器)是

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

Interface:   Intel(R) Dual Band Wireless-AC 7260 (wlan1)
IP address:  192.168.2.105
MAC:         xxxxxxxxxxxx