MAC地址的问题是可以有很多网络适配器连接到计算机。大多数最新的默认情况下都有两个(wi-fi + 电缆)。在这种情况下,必须知道应该使用哪个适配器的 MAC 地址。我在我的系统上测试了 MAC 解决方案,但我有 4 个适配器(用于 Virtual Box 的电缆、WiFi、TAP 适配器和一个用于蓝牙),我无法决定我应该使用哪个 MAC... 如果有人决定使用适配器当前正在使用(已分配地址)然后出现新问题,因为有人可以拿起他/她的笔记本电脑并从电缆适配器切换到wi-fi。在笔记本电脑通过电缆连接时存储的这种情况下,MAC 现在将无效。
例如,这些是我在系统中找到的适配器:
lo MS TCP Loopback interface
eth0 Intel(R) Centrino(R) Advanced-N 6205
eth1 Intel(R) 82579LM Gigabit Network Connection
eth2 VirtualBox Host-Only Ethernet Adapter
eth3 Sterownik serwera dostepu do sieci LAN Bluetooth
我用来列出它们的代码:
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
System.out.println(ni.getName() + " " + ni.getDisplayName());
}
从这个页面上的选项听,我最能接受,我在我的解决方案中使用的是@Ozhan Duz 的一个,另一个类似于@finnw answer where he used JACOB,并且值得一提的是com4j - 使用 WMI 的示例可用here:
ISWbemLocator wbemLocator = ClassFactory.createSWbemLocator();
ISWbemServices wbemServices = wbemLocator.connectServer("localhost","Root\\CIMv2","","","","",0,null);
ISWbemObjectSet result = wbemServices.execQuery("Select * from Win32_SystemEnclosure","WQL",16,null);
for(Com4jObject obj : result) {
ISWbemObject wo = obj.queryInterface(ISWbemObject.class);
System.out.println(wo.getObjectText_(0));
}
这将打印一些计算机信息以及计算机序列号。请注意,此示例所需的所有类都必须由 maven-com4j-plugin 生成。 maven-com4j-plugin 的示例配置:
<plugin>
<groupId>org.jvnet.com4j</groupId>
<artifactId>maven-com4j-plugin</artifactId>
<version>1.0</version>
<configuration>
<libId>565783C6-CB41-11D1-8B02-00600806D9B6</libId>
<package>win.wmi</package>
<outputDirectory>${project.build.directory}/generated-sources/com4j</outputDirectory>
</configuration>
<executions>
<execution>
<id>generate-wmi-bridge</id>
<goals>
<goal>gen</goal>
</goals>
</execution>
</executions>
</plugin>
上面的配置会告诉插件在项目文件夹的target/generated-sources/com4j目录下生成类。
对于那些希望看到即用型解决方案的人,我提供了我为在 Windows、Linux 和 Mac OS 上获取机器 SN 而编写的三个类的链接: