Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NetworkInterface.getNetworkInterfaces()未列出所有接口_Java - Fatal编程技术网

Java NetworkInterface.getNetworkInterfaces()未列出所有接口

Java NetworkInterface.getNetworkInterfaces()未列出所有接口,java,Java,我的机器上有三个接口(eth0、Loopback、wlan0),我想使用javaapi获取mac地址 我使用这个代码 Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) displayInterfaceInformation(netint); }

我的机器上有三个接口
(eth0、Loopback、wlan0)
,我想使用javaapi获取mac地址

  • 我使用这个代码

    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }
    
    static void displayInterfaceInformation(NetworkInterface netint) 
      throws SocketException 
    {
        System.out.println("Display name: " 
           + netint.getDisplayName());
        System.out.println("Hardware address: " 
           + Arrays.toString(netint.getHardwareAddress()));
    }
    

    ifconfig

    $ ifconfig
    eth0      Link encap:Ethernet  HWaddr -------------  
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:1695 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1695 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:129949 (129.9 KB)  TX bytes:129949 (129.9 KB)
    
    wlan0     Link encap:Ethernet  HWaddr -------------------  
              inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::-------------- Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:8396 errors:0 dropped:0 overruns:0 frame:0
              TX packets:5524 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:3959941 (3.9 MB)  TX bytes:1513934 (1.5 MB)
    

    调用
    getNetworkInterfaces
    Java时将返回

    
    这台机器上的所有接口。如果在此计算机上找不到网络接口,则返回null。
    

    你不是唯一一个这样的人。显然,在linux上,Java将只返回分配了IP地址的接口(即配置的适配器)


    但是从应用程序的角度来看(除非您正在构建网络配置应用程序),拥有一个没有IP地址的接口就像根本没有IP地址一样。您必须轮询接口或在每次访问应用程序中的“网络首选项”时获取接口。

    显然,我一开始就错了:即使
    ifconfig
    和Java API都在使用 同样的
    ioctl()
    syscalls,它们的行为不同

    首先,
    SIOCGIFCONF ioctl()
    记录如下(请参阅):


    扩展@Andreas answer,我们可以编写一个小的shell脚本,比如
    $ifconfg | grep“Link encap”>一些_文件
    ,然后将有一个更小的(只有3行)文件来解析和拾取每行上的第一个标记。获得HWaddress也是一样的事情。我们将编写更少的java代码


    其他选项可以是使用ApacheCommons
    IOUtils.toString(新文件输入流(,US_ASCII))
    读取设置。这将消除其解决方案中重复的java I/O代码。

    是否配置了eth0?当你执行
    ifconfig
    时,它会显示吗?你的代码对我有效(Ubuntu 12.04 LTS)-它显示
    lo
    eth0
    ,正如@Erik所说:
    ifconfig
    的输出是什么?
    ifconfig
    和javaapi最终都在使用相同的
    ioctl
    调用(
    SIOCGIFCONF
    ),因此它们应该consistent@ErikEkman我的机器操作系统Ubuntu和eth0显示在ifconfig中。@Andreas我在sony vaio笔记本电脑上有相同的操作系统(Ubuntu12.04 LTS)。此代码仅显示给我(lo,wlan0).Hey@Andreas我正在为OS X寻找类似的解决方案。。[这里的问题]()。我真的非常感谢你的帮助。谢谢
    $ ifconfig
    eth0      Link encap:Ethernet  HWaddr -------------  
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:1695 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1695 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:129949 (129.9 KB)  TX bytes:129949 (129.9 KB)
    
    wlan0     Link encap:Ethernet  HWaddr -------------------  
              inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::-------------- Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:8396 errors:0 dropped:0 overruns:0 frame:0
              TX packets:5524 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:3959941 (3.9 MB)  TX bytes:1513934 (1.5 MB)
    
    SIOCGIFCONF Return a list of interface (transport layer) addresses. ... The kernel fills the ifreqs with all current L3 interface addresses that are running.
    static void printHardwareAddresses() throws SocketException {
        if (System.getProperty("os.name").equals("Linux")) {
    
            // Read all available device names
            List<String> devices = new ArrayList<>();
            Pattern pattern = Pattern.compile("^ *(.*):");
            try (FileReader reader = new FileReader("/proc/net/dev")) {
                BufferedReader in = new BufferedReader(reader);
                String line = null;
                while( (line = in.readLine()) != null) {
                    Matcher m = pattern.matcher(line);
                    if (m.find()) {
                        devices.add(m.group(1));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // read the hardware address for each device
            for (String device : devices) {
                try (FileReader reader = new FileReader("/sys/class/net/" + device + "/address")) {
                    BufferedReader in = new BufferedReader(reader);
                    String addr = in.readLine();
    
                    System.out.println(String.format("%5s: %s", device, addr));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        } else {
            // use standard API for Windows & Others (need to test on each platform, though!!)
            ...
        }
    }