Many times, we need to obtain server hardware information (such as MAC address). There are several commonly used methods:
•Use the command line program to obtain the hard disk information, then obtain the output stream through Runtime.getRuntime().exec, and then obtain the MAC address through string analysis
•Compile the local program and then call it through JNI
Both of the above methods require distinguishing different operating system platforms and encoding them separately, which is more troublesome, such as
• Windows platform needs to use iptables /all command
•Linux platform needs to use ifconfig command
Today I will introduce a common cross-platform operation method, which is the NetworkInterface interface that comes with JDK. This interface has appeared in JDK1.4, but has relatively few functions. After JDK1.6, many new functions have been added, which is quite good.
You can refer to the API documentation for specific functions. Here we mainly introduce how to obtain the server MAC address. The code is as follows, with comments, so I won’t go into details.
Copy the code code as follows:
//Get the MAC addresses of all network cards
public static List<String> getAllMac() {
List<String> list = new ArrayList<String>();
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); // Returns an enumeration instance of all network interfaces
while (e.hasMoreElements()) {
NetworkInterface network = e.nextElement();//Get the current network interface
if (network != null) {
if (network.getHardwareAddress() != null) {
// Get MAC address
//The result is a byte array, each item is a byte, we need to convert it into a common hexadecimal representation through the parseByte method
byte[] addres = network.getHardwareAddress();
StringBuffer sb = new StringBuffer();
if (addres != null && addres.length > 1) {
sb.append(parseByte(addres[0])).append(":").append(
parseByte(addres[1])).append(":").append(
parseByte(addres[2])).append(":").append(
parseByte(addres[3])).append(":").append(
parseByte(addres[4])).append(":").append(
parseByte(addres[5]));
list.add(sb.toString());
}
}
} else {
System.out.println("An exception occurred while obtaining the MAC address");
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return list;
}
//Format binary
private static String parseByte(byte b) {
int intValue = 0;
if (b >= 0) {
intValue = b;
} else {
intValue = 256 + b;
}
return Integer.toHexString(intValue);
}
Then, we use the following test code to see the test results
Copy the code code as follows:
List<String> list = getAllMac();
for (String mac : list) {
System.out.println(mac);
}
The output is as follows:
Copy the code code as follows:
0:18:8b:cc:xx:e3
0:0:0:0:0:0:0:e0
0:50:xx:c0:0:1
0:50:xx:c0:0:8
Everyone found that "0:18:8b:cc:xx:e3" has only one 0, which looks very awkward. We might as well revise the parseByte method as follows:
Copy the code code as follows:
private static String parseByte(byte b) {
String s = "00"+Integer.toHexString(byte0);
return s.substring(s.length() - 2);
}
The output results change:
Copy the code code as follows:
00:18:8b:cc:xx:e3
00:00:00:00:00:e0
00:50:xx:c0:00:01
00:50:xx:c0:00:08
It looks more comfortable this way, right?
In addition, the NetworkInterface interface also provides the following methods, which you can refer to.
•String displayName() Gets the display name of the network interface
•int getMTU() returns the Maximum Transmission Unit (MTU) of this interface
•String getName() gets the name of this network interface
•boolean isLoopback() returns whether this network interface is a loopback interface
•boolean isPointToPoint() returns whether this network interface is a point-to-point interface
•boolean isUp() returns whether this network interface is up and running
•boolean isVirtual() returns whether this interface is a virtual interface