从es代码里面看到的, 注意, 一个机器可能有多个mac地址, 代码中取出第一个有效的
private static byte[] getMacAddress() throws SocketException {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
if (en != null) {
while (en.hasMoreElements()) {
NetworkInterface nint = en.nextElement();
if (!nint.isLoopback()) {
// Pick the first valid non loopback address we find
byte[] address = nint.getHardwareAddress();
if (isValidAddress(address)) {
return address;
}
}
}
}
// Could not find a mac address
return null;
}
private static boolean isValidAddress(byte[] address) {
if (address == null || address.length != 6) {
return false;
}
for (byte b : address) {
if (b != 0x00) {
return true; // If any of the bytes are non zero assume a good
// address
}
}
return false;
}