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

时间:2017-09-20 11:56:56

标签: java windows mac-address serial-number

许多主板都有一个集成的网络适配器。如果存在,我需要从该设备获取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());
        }
}

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

4 个答案:

答案 0 :(得分:3)

16.0.0

并获得硬件信息你可以使用像wmic这样的命令 wmic cpu get ProcessorId

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

和bios

wmic bios获取SerialNumber

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

答案 1 :(得分:1)

  

如何从设备获取mac地址?

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

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

  1. 使用ipconfig命令处理此问题。
  2. 使用nbtstat命令检索远程计算机的mac地址。
  3. 运行getmacgetmac /s remote_computer /u username /p password命令以检索远程计算机的mac地址。
  4. 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

答案 2 :(得分:0)

使用UUID进行一些更改

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

答案 3 :(得分:0)

我建议您查看oshi library以获取系统操作系统/硬件信息。

查看示例的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