使用java在mac os上获取常用mac地址
问题描述:
我正在构建一个java应用程序,该应用程序获取用户的mac地址并将其与数据库中的对应值(安全功能)进行比较。但问题发生在Mac OS上,当我发现MAC地址列表具有共同的价值(例如:在我的MAC地址列表的MAC地址是:001C42000009,001C42000008,E0F8474267B6(wifi),70CD60F1A5C1(以太网)) 有没有一个方法来知道在Mac OS上获取Mac地址时将导致的所有这些常见值。使用java在mac os上获取常用mac地址
谢谢。
答
在http://standards.ieee.org/develop/regauth/oui/public.html您可以使用MAC地址的前3个字节查找供应商,00-1C-42点“的Parallels公司“ (http://www.parallels.com)。你在使用他们的一些虚拟化软件吗?试试java.net.NetworkInterface.isVirtual()
为此地址返回的内容,如果没有用,那么一些难看的过滤器可能需要(例如基于地址模式)
import java.net.NetworkInterface;
import java.util.Enumeration;
public class NetworkInterfaceTest {
public static void main(String args[]) {
try {
Enumeration<NetworkInterface> ie = NetworkInterface.getNetworkInterfaces();
while (ie.hasMoreElements()) {
NetworkInterface i = ie.nextElement();
System.out.println(i.getDisplayName() + " [" + i.getName() + "]: " + formatAddress(i.getHardwareAddress()) + "; isVirtual=" + i.isVirtual());
}
} catch (Exception e){
e.printStackTrace();
}
}
private static String formatAddress(byte[] address) {
if (address == null) {
return null;
}
StringBuilder ret = new StringBuilder(address.length * 2);
for (byte b : address) {
if (ret.length() > 0) {
ret.append('-');
}
String bs = Integer.toHexString(b & 0x000000FF).toUpperCase();
if (bs.length() < 2) {
ret.append('0');
}
ret.append(bs);
}
return ret.toString();
}
}
答
我相信这样的事情会做的工作对你
try {
InetAddress []addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
/*
* Get NetworkInterfaces for current host and read hardware addresses.
*/
for(int j=0; j< addresses.length; i++) {
System.out.format("%02X%s", mac[i], (i < addresses.length – 1) ? "-" : "");
}
System.out.println();
}
}