1. Introduction
1.绪论
In this tutorial, we’ll use Java to get the MAC addresses of a local machine.
在本教程中,我们将使用Java来获取本地机器的MAC地址。
A MAC address is the unique identifier for a physical network interface card.
MAC地址是一个物理网络接口卡的唯一标识符。
We’ll cover MAC addresses only, but for a more general overview of network interfaces, refer to Working with Network Interfaces in Java.
我们将只讨论MAC地址,但要了解网络接口的一般概况,请参考在Java中使用网络接口。
2. Examples
2.实例
In our examples below, we’ll make use of the java.net.NetworkInterface and java.net.InetAddress APIs.
在我们下面的例子中,我们将利用java.net.NetworkInterface和java.net.InetAddress APIs。
2.1. Machine Localhost
2.1. Localhost机器
First, let’s get the MAC address for our machine’s localhost:
首先,让我们得到我们机器的本地主机的MAC地址。
InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(localHost);
byte[] hardwareAddress = ni.getHardwareAddress();
As NetworkInterface#getHardwareAddress returns an array of bytes, we can format the result:
由于NetworkInterface#getHardwareAddress返回一个字节数组,我们可以将结果格式化。
String[] hexadecimal = new String[hardwareAddress.length];
for (int i = 0; i < hardwareAddress.length; i++) {
hexadecimal[i] = String.format("%02X", hardwareAddress[i]);
}
String macAddress = String.join("-", hexadecimal);
Notice how we format each byte in the array to a hexadecimal number using String#format.
注意我们是如何使用String#format将数组中的每个字节格式化为一个十六进制数字的。
After that, we can join all the formatted elements with a “-” (dash).
之后,我们可以用”-“(破折号)连接所有的格式化元素。
2.2. Local IP
2.2.本地IP
Secondly, let’s get the MAC address for a given local IP address:
其次,让我们来获取一个给定的本地IP地址的MAC地址。
InetAddress localIP = InetAddress.getByName("192.168.1.108");
NetworkInterface ni = NetworkInterface.getByInetAddress(localIP);
byte[] macAddress = ni.getHardwareAddress();
Again, notice how we get an array of bytes for the MAC address.
同样,注意到我们是如何得到一个MAC地址的字节数组的。
2.3. All Network Interfaces
2.3.所有网络接口
Finally, let’s get the MAC addresses for all the network interfaces on our machine:
最后,让我们得到我们机器上所有网络接口的MAC地址。
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface ni = networkInterfaces.nextElement();
byte[] hardwareAddress = ni.getHardwareAddress();
if (hardwareAddress != null) {
String[] hexadecimalFormat = new String[hardwareAddress.length];
for (int i = 0; i < hardwareAddress.length; i++) {
hexadecimalFormat[i] = String.format("%02X", hardwareAddress[i]);
}
System.out.println(String.join("-", hexadecimalFormat));
}
}
As getNetworkInterfaces returns both physical and virtual interfaces, we need to filter out the virtual ones.
由于getNetworkInterfaces同时返回物理和虚拟接口,我们需要过滤掉虚拟的接口。
We can do this for example, by doing a null check on getHardwareAddress.
例如,我们可以通过对getHardwareAddress进行null检查来做到这一点。
3. Conclusion
3.总结
In this quick tutorial, we explored different ways of getting MAC addresses for a local machine.
在这个快速教程中,我们探讨了获取本地机器MAC地址的不同方法。
As usual, all the source code with the examples in this tutorial can be found over on GitHub.
像往常一样,本教程中的所有例子的源代码都可以在GitHub上找到over。