java - java获取物理mac地址

标签 java windows virtual-machine exe

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

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

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

最佳答案

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

编辑1: 我修改了你的例子。对于您使用的类,我能想到的唯一方法是使用适配器名称。我有一些源自虚拟机的适配器。它们的名称中都有VirtualBoxHyper-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

关于java - java获取物理mac地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42504037/

相关文章:

在控制台中看不到 Windows 程序的标准输出(在 Windows 上用 Clang 编译)

windows - 如何在远程 Windows 服务器桌面计算机上重置密码?

windows - 无法使用 VMware windows 7 作为主操作系统和 centOS 6 作为 guest 同步共享文件夹

java - 用于检查 Selenium WebDriver 中项目列表的循环

java - String 到 Long 的转换在 C 和 Java 中不同,为什么?

java - aws将目录从一个位置递归复制到同一s3存储桶中的另一个位置

c++ - 在 x86 和 x64 体系结构中获取 Windows 序列号 (MachineGuid)

java - 将客户端信息发送到 ejbs

linux - 用两个程序读取一个串口 : Python 3

rest - 是否有机会使用 rest API 在 ManageIQ 中创建 VM?