Linux无法获取外部网络,无法使用Linux上的NetworkInterface.getNetworkInterfaces获取所有networking接口...

我需要打印我的机器的所有mac地址。 推荐的方法是使用NetworkInterface.getNetworkInterfaces()并遍历返回的枚举。 但是,当一些设备closures(没有IPconfiguration),那么上述方法将不会返回接口。

运行“ip addr”将返回以下内容

lo:mtu 16436 qdisc noqueue link / loopback 00:00:00:00:00 brd 00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 :: 1/128 scope host valid_lft永远forever_lft永远

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

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范围链接valid_lft永远preferred_lft永远

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

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

但是,当我运行Java代码(即使作为根或具有networking权限),我只得到回送和G1接口。

以下是我为testing目的而编写的代码:

Enumeration 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解决scheme。

有什么想法吗 ?

看起来像“IP地址”显示所有的网络适配器,但不是所有的网络地址配置。 所以,Java只返回网络接口,即配置的适配器。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值