如何从集成网络适配器获取 mac 地址

How to get mac address from integrated network adapter

许多主板都集成了网络适配器。如果存在,我需要从该设备获取 mac 地址。 来自网络适配器:

private void getMacFromInetAddress(){
    try {

            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            while (networks.hasMoreElements()) {
                NetworkInterface network = networks.nextElement();
                byte[] mac = network.getHardwareAddress();

                if (mac != null) {
                    System.out.print("Current MAC address : ");

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                }
           }
        } catch (UnknownHostException | SocketException e) {
            System.out.println(e.getLocalizedMessage());
        }
}

如何从这段代码中指定集成网络适配器的名称?

使用 UUID 进行一些更改

第一次 运行s,生成一个唯一的 UUID 并将其保存在本地存储中。从第 2 次开始使用从第 1 次 运行 保存的 UUID。

How to get mac address from device?

没有通过 Java 从系统中获取 mac address 的直接方法。但是你可以

要在计算机上获取 mac 个地址,只需 运行 命令 getmac 即可获取 mac 个地址。

  1. 使用ipconfig命令来处理。
  2. 使用 nbtstat 命令检索远程计算机的 mac 地址。
  3. 运行 getmacgetmac /s remote_computer /u username /p password 命令检索远程计算机的 mac 地址。

Get mac address from command line

但是通过Java你需要使用Java exec system with Java ProcessBuilder and Process

例如:

ProcessBuilder pb = new ProcessBuilder("getmac");
//Map<String, String> env = pb.environment(); //set env
pb.directory("C\");
Process p = pb.start();

// build my command as a list of strings
//@@@ For Unix/Linux
List<String> command = new ArrayList<String>();
command.add("getmac");

// execute my command
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(command);
int result = commandExecutor.executeCommand();

然后通过读取输出:

// get the output from the command
StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
StringBuilder stderr = commandExecutor.getStandardErrorFromCommand();

// print the output from the command
System.out.println("STDOUT");
System.out.println(stdout);
System.out.println("STDERR");
System.out.println(stderr);

ProcessBuilder and Process Code

Note: 你的 java application 需要 administrative permission during execution.

InetAddress ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());

NetworkInterface network = NetworkInterface.getByInetAddress(ip);

byte[] mac = network.getHardwareAddress();

System.out.print("Current MAC address : ");

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(sb.toString());

要获取硬件信息,您可以使用 wmic 命令,例如 wmic cpu 获取 ProcessorId

 Process p = Runtime.getRuntime().exec("wmic cpu get ProcessorId");
            String procesSerial=
                    new BufferedReader
                            (new InputStreamReader(p.getInputStream())).[ReadSecondLine];

和 BIOS

wmic bios 获取序列号

 Process p = Runtime.getRuntime().exec("wmic bios get SerialNumber");

我建议您查看 oshi library 以获取系统 OS/hardware 信息。

查看 test case 示例。

以下是我系统上的截断输出。

Microsoft Windows 7 SP1 build 7601
manufacturer: LENOVO
model: 20AWA0MAIN
serialnumber: PB01FEYL
baseboard:
  manufacturer: LENOVO
  serialnumber: L1HF43B026Z               
Intel(R) Core(TM) i7-4600M CPU @ 2.90GHz
 2 physical CPU(s)
 4 logical CPU(s)
Identifier: Intel64 Family 6 Model 60 Stepping 3
ProcessorID: BFEBFBFF000306C3
Memory: 9.2 GiB/15.7 GiB
Swap used: 0 bytes/15.7 GiB
Uptime: 0 days, 04:24:20
Disks:
 \.\PHYSICALDRIVE0: (model: HGST HTS725050A7E6300 SCSI Disk Device (Standard disk drives) - S/N:       TF655AWH2RKE6L) size: 500.1 GB, reads: 325475 (7.1 GiB), writes: 306825 (4.4 GiB), xfer: 12447064 ms
 |-- Disk #0, Partition #0: Installable File System (Installable File System) Maj:Min=0:0, size: 104.9 MB
Network interfaces:
 Name: eth8 (Intel(R) Ethernet Connection I217-LM)
   MAC Address: 28:d2:44:68:40:23 
   MTU: 1500, Speed: 0 bps 
   IPv4: [] 
   IPv6: [fe80:0:0:0:553d:8bc9:6a95:236e] 
   Traffic: received ?/?; transmitted ?/? 
 Name: wlan9 (Intel(R) Dual Band Wireless-N 7260)
   MAC Address: 7c:7a:91:37:cb:f7 
   MTU: 1500, Speed: 130 Mbps 
   IPv4: [192.168.1.66] 
   IPv6: [fe80:0:0:0:55e0:97b3:f282:4c06] 
   Traffic: received 121644 packets/114.0 MiB (0 err); transmitted 90086 packets/12.7 MiB (0 err) 
Displays:
 Display 0:
  Manuf. ID=LEN, Product ID=40a0, Analog, Serial=00000000, ManufDate=1/2012, EDID v1.4
  31 x 17 cm (12.2 x 6.7 in)
  Preferred Timing: Clock 72MHz, Active Pixels 2656x768 
  Preferred Timing: Clock 63MHz, Active Pixels 2656x768 
  Manufacturer Data: 0000000F008C09328C093214090006AF3C33
  Unspecified Text: B140XTN03.3