获取 Java 中特定网络接口的名称

Get name of a specific network interface in Java

我正在使用 windows 10,我想在我的计算机上插入一个设备(它创建了一个新的网络接口,名称为 "Ethernet 12",描述为 "foobar")并且可以使用 Java 设置此接口的 IP 地址。使用此 link: Change Computer IP address using JAVA,我可以配置接口的 IP 地址。我遇到的问题是我无法在 java 中检索设备创建的接口的名称。根据我找到的文档,我执行了以下操作:

    Enumeration<NetworkInterface> networkInterfaces =  NetworkInterface.getNetworkInterfaces();

    while(networkInterfaces.hasMoreElements())
    {
        NetworkInterface networkInterface = networkInterfaces.nextElement();

        if (networkInterface.isUp())
            System.out.println("Display name: " + networkInterface.getDisplayName() + "\nName: " + networkInterface.getName() + "\nAddress: " + networkInterface.getInterfaceAddresses().get(0));
    }

得到以下结果:

    Display name: foobar
    Name: eth15
    Address 111.116.15.4

我希望名字是"Ethernet 12"而不是"eth15",这似乎没有任何意义。我注意到所有其他接口(wifi、以太网等)都一样。由于我需要我的接口(以太网 12)的真实名称来使用 netsh 配置 IP 地址(而不是像 "eth15" 这样的随机地址),我被卡住了...

我将 "eth15" 作为我的界面名称有什么原因吗?

谢谢

Is there a reason why I am getting "eth15" as the name of my interface ?

这需要一些挖掘。在 Java 世界中,NetworkInterfacesjava.net.NetworkInterface::getNetworkInterfaces() 枚举,它调用 native java.net.NetworkInterface::getAll()

我们可以在 OpenJDK 11 here 中找到 getAll() 的本机源代码,我们在其中找到了这条评论:

/*
 * Windows implementation of the java.net.NetworkInterface native methods.
 * This module provides the implementations of getAll, getByName, getByIndex,
 * and getByAddress.
 *
 * Interfaces and addresses are enumerated using the IP helper routines
 * GetIfTable, GetIfAddrTable resp. These routines are available on Windows
 * 98, NT SP+4, 2000, and XP. They are also available on Windows 95 if
 * IE is upgraded to 5.x.
 *
 * Windows does not have any standard for device names so we are forced
 * to use our own convention which is based on the normal Unix naming
 * convention ("lo" for the loopback, eth0, eth1, .. for ethernet devices,
 * tr0, tr1, .. for token ring, and so on). This convention gives us
 * consistency across multiple Windows editions and also consistency with
 * Solaris/Linux device names. Note that we always enumerate in index
 * order and this ensures consistent device number across invocations.
 */

所以曾几何时,当 Windows 95 仍然存在时,有人决定不阅读实际的 Windows 接口名称,而是使用 "our own convention"。为什么?不知道。我更希望得到一个单独的 deviceName() 和一个 ourOwnConventionWhichHasNothingToDoWithTheActualNameDeviceName(),但不幸的是我没有被咨询。

底层WindowsAPI调用GetIfTable does return a name for each interface in the form an array of MIB_IFROW:

typedef struct _MIB_IFROW {
  WCHAR                   wszName[MAX_INTERFACE_NAME_LEN];
  IF_INDEX                dwIndex;
  IFTYPE                  dwType;
  DWORD                   dwMtu;
  DWORD                   dwSpeed;
  DWORD                   dwPhysAddrLen;
  UCHAR                   bPhysAddr[MAXLEN_PHYSADDR];
  DWORD                   dwAdminStatus;
  INTERNAL_IF_OPER_STATUS dwOperStatus;
  DWORD                   dwLastChange;
  DWORD                   dwInOctets;
  DWORD                   dwInUcastPkts;
  DWORD                   dwInNUcastPkts;
  DWORD                   dwInDiscards;
  DWORD                   dwInErrors;
  DWORD                   dwInUnknownProtos;
  DWORD                   dwOutOctets;
  DWORD                   dwOutUcastPkts;
  DWORD                   dwOutNUcastPkts;
  DWORD                   dwOutDiscards;
  DWORD                   dwOutErrors;
  DWORD                   dwOutQLen;
  DWORD                   dwDescrLen;
  UCHAR                   bDescr[MAXLEN_IFDESCR];
} MIB_IFROW, *PMIB_IFROW;

JVM 似乎read this array, then generate an "appropriate" name, and finally override the OS-provided name with the generated one

简而言之,它的实现看起来已经很多年没有变化了,如果不创建一个全新的 API,现在可能不可能改变。否则你可能会破坏他人的代码。

在我看来,有两种获取实际接口名称的方法:

  1. 使用 JNI 自己进行本地调用 GetIfTable
  2. 执行 netsh 并解析响应。

这两种解决方案都有点丑陋,需要您确保 运行 在 Windows。