无法在Linux上使用NetworkInterface.getNetworkInterfaces获取所有网络接口 [英] Unable to get all network interfaces with NetworkInterface.getNetworkInterfaces on Linux

查看:163
本文介绍了无法在Linux上使用NetworkInterface.getNetworkInterfaces获取所有网络接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印机器的所有mac地址。建议的方法是使用NetworkInterface.getNetworkInterfaces()并迭代返回的枚举。但是当某些设备关闭时(NO Ip已配置),上述方法将不会返回接口。

I need to print all the mac addresses of my machine. The recommended way is to use NetworkInterface.getNetworkInterfaces() and iterate on the enumeration returned. However when some devices are down ( NO Ip is configured ) then the above method will not return the interfaces.


  1. lo:mtu 16436 qdisc noqueue
    link / loopback 00:00:00:00:00:00 brd 00:00:00:00 :00:00
    inet 127.0.0.1/8范围主机lo
    inet6 :: 1/128范围主机
    valid_lft forever preferred_lft forever

  2. G2: mtu 1500 qdisc pfifo_fast qlen 1000
    link / ether 00:03:b2:75:99:c2 brd ff:ff:ff:ff:ff:ff

  3. G1:mtu 1500 qdisc pfifo_fast qlen 1000
    link / ether 00:03:b2:75:99:c3 brd ff:ff:ff:ff:ff:ff
    inet 10.205.191.123/16 brd 10.205.255.255范围全球G1
    inet6 fe80 :: 203:b2ff:fe75:99c3 / 64范围链接
    valid_lft forever preferred_lft forever

  4. eth2:mtu 1500 qdisc noop qlen 1000
    link / ether 00:03:b2:75:99:c4 brd ff:ff:ff:ff:ff:ff

  5. eth3:mtu 1500 qdisc noop qlen 1000
    link / ether 00:03: b2:75:99:c5 brd ff:ff:ff:ff:ff:ff

  1. lo: mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever
  2. G2: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:03:b2:75:99:c2 brd ff:ff:ff:ff:ff:ff
  3. G1: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:03:b2:75:99:c3 brd ff:ff:ff:ff:ff:ff inet 10.205.191.123/16 brd 10.205.255.255 scope global G1 inet6 fe80::203:b2ff:fe75:99c3/64 scope link valid_lft forever preferred_lft forever
  4. eth2: mtu 1500 qdisc noop qlen 1000 link/ether 00:03:b2:75:99:c4 brd ff:ff:ff:ff:ff:ff
  5. eth3: mtu 1500 qdisc noop qlen 1000 link/ether 00:03:b2:75:99:c5 brd ff:ff:ff:ff:ff:ff



但是当我运行Java代码时(甚至作为root或网络权限)我只获得环回和G1接口。



这是我为测试目的编写的代码:

However when I run Java code (even as root or with network privileges) I only get the loopback and G1 interfaces.

Here is the code I wrote for testing purposes :

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)。

The output is: 00:03:B2:75:99:C3 (of G1) only.

如果可能,我确实想要一个纯java解决方案。

有什么想法吗?

I do want a pure java solution if possible.
Any thoughts ?

推荐答案

看起来ip addr显示所有网络适配器,但并非所有网络适配器都配置了互联网地址。因此,Java只返回网络接口,即配置的适配器。

Looks like the "ip addr" shows all network adapters, but not all are configured with an internet address. So, the Java returns only network interfaces, i.e. configured adapters.

这篇关于无法在Linux上使用NetworkInterface.getNetworkInterfaces获取所有网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆