簡體   English   中英

在Java中獲取物理Mac地址

[英]Get physical mac address in java

我在使用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();
}

我一直在尋找的其他主題使用Java在本地計算機獲取MAC地址

用Java獲取系統的MAC地址

我想要的只是獲得以太網適配器網絡。我隨硬件附帶的物理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(除非您設法將實際設備從主機傳遞到VM)。

編輯1:我修改了您的示例。 對於這些類,您唯一想到的方法就是使用適配器名稱。 我有一些源自VM的適配器。 它們的名稱都帶有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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM