[英]NetworkInterface.getNetworkInterfaces() not listing all interfaces
[英]Unable to get all network interfaces with NetworkInterface.getNetworkInterfaces on Linux
我需要打印機器的所有mac地址。 建議的方法是使用NetworkInterface.getNetworkInterfaces()並迭代返回的枚舉。 但是,當某些設備關閉時(NO Ip已配置),則上述方法將不會返回接口。
這是我為測試目的編寫的代碼:
Enumeration<NetworkInterface> ni = NetworkInterface.getNetworkInterfaces();
while(ni.hasMoreElements()){
NetworkInterface nextElement = ni.nextElement();
byte[] mac = nextElement.getHardwareAddress();
if (mac != null) {
StringBuffer macAddress = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
System.out.println(macAddress.toString());
}
}
輸出為: 00:03:B2:75:99:C3 (僅G1)。
如果可能,我確實想要一個純java解決方案。
有什么想法嗎 ?
看起來“ip addr”顯示所有網絡適配器,但並非所有網絡適配器都配置了Internet地址。 因此,Java只返回網絡接口,即配置的適配器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.