如何在 Java 中访问每个网络接口的特定于连接的 DNS 后缀?

发布于 2024-11-10 02:54:30 字数 722 浏览 3 评论 0原文

Java 程序中是否可以访问 Windows 计算机的 ipconfig /all 输出的“特定于连接的 DNS 后缀”字段中包含的字符串?

例如:

C:>ipconfig /all

以太网适配器本地连接:

    Connection-specific DNS Suffix  . : myexample.com  <======== This string
    Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet
    Physical Address. . . . . . . . . : 00-30-1B-B2-77-FF
    Dhcp Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    IP Address. . . . . . . . . . . . : 192.168.1.66
    Subnet Mask . . . . . . . . . . . : 255.255.255.0

我知道 getDisplayName() 将返回描述(例如:上面的 Broadcom NetXtreme 千兆位以太网)并且 getInetAddresses() 将为我提供绑定的 IP 地址列表到此网络接口。

但是还有读取“特定于连接的 DNS 后缀”的方法吗?

Is it possible within a Java program to access the string contained in the "Connection-specific DNS Suffix" field of a Windows machine's ipconfig /all output?

Eg:

C:>ipconfig /all

Ethernet adapter Local Area Connection:

    Connection-specific DNS Suffix  . : myexample.com  <======== This string
    Description . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet
    Physical Address. . . . . . . . . : 00-30-1B-B2-77-FF
    Dhcp Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    IP Address. . . . . . . . . . . . : 192.168.1.66
    Subnet Mask . . . . . . . . . . . : 255.255.255.0

I know that getDisplayName() will get return the Description (Eg: Broadcom NetXtreme Gigabit Ethernet above) and that getInetAddresses() will give me a list of the IP addresses bound to this network interface.

But are there also ways of reading the "Connection-specific DNS Suffix"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2024-11-17 02:54:30

好的,我想出了如何在 Windows XP 和 Windows 7 上执行此操作:

  • 字符串(例如:myexample.com)
    包含在特定于连接的
    每个网络的 DNS 后缀字段
    输出中列出的接口
    ipconfig /all 可以在
    登记处
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces{GUID}
    (其中 GUID 是
    感兴趣的网络接口)作为
    名为 DhcpDomain 的字符串值(类型 REG_SZ)。
  • 在 Java 中访问 Windows 注册表项并不简单,但通过巧妙地使用反射,可以访问 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\ 下找到的所需网络适配器的项,然后读取字符串数据元素名称 Dhcp 域;它的值是所需的字符串。
  • 请参阅以下链接查看示例
    访问Windows注册表
    来自爪哇:

Ok so I figured out how to do this on Windows XP and Windows 7:

  • The string (eg: myexample.com)
    contained in the Connection-specific
    DNS Suffix field of each network
    interface listed in the output of
    ipconfig /all can be found in the
    registry at
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces{GUID}
    (where GUID is the GUID of the
    network interface of interest) as the
    string value (type REG_SZ) named DhcpDomain.
  • Accessing windows registry keys is not straightforward in Java but by some clever use of reflection it is possible to access the required network adaptor's key found under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\ and then read the string data element with name DhcpDomain; its value is the required string.
  • See the following links for examples
    of accessing the windows registry
    from Java:

肥爪爪 2024-11-17 02:54:30

我使用了一种更复杂的方法,它适用于所有平台。

在 Windows 7、Ubuntu 12.04 和一些未知的 Linux 发行版(Jenkins 构建主机)和一台 MacBook(未知的 MacOS X 版本)上进行了测试。

始终使用 Oracle JDK6。从未与其他 VM 供应商进行过测试。

String findDnsSuffix() {

// First I get the hosts name
// This one never contains the DNS suffix (Don't know if that is the case for all VM vendors)
String hostName = InetAddress.getLocalHost().getHostName().toLowerCase();

// Alsways convert host names to lower case. Host names are 
// case insensitive and I want to simplify comparison.

// Then I iterate over all network adapters that might be of interest
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();

if (ifs == null) return ""; // Might be null

for (NetworkInterface iF : Collections.list(ifs)) { // convert enumeration to list.
    if (!iF.isUp()) continue;

    for (InetAddress address : Collections.list(iF.getInetAddresses())) {
        if (address.isMulticastAddress()) continue;

        // This name typically contains the DNS suffix. Again, at least on Oracle JDK
        String name = address.getHostName().toLowerCase();

        if (name.startsWith(hostName)) {
            String dnsSuffix = name.substring(hostName.length());
            if (dnsSuffix.startsWith(".")) return dnsSuffix;
        }
    }
}

return "";
}

注意:我在编辑器中编写了代码,没有复制实际使用的解决方案。它还不包含任何错误处理,例如没有名称的计算机、无法解析 DNS 名称等。

I used a much more complicated approach, which works on all platforms.

Tested on Windows 7, Ubuntu 12.04 and some unknown Linux distributions (Jenkins build hosts) and one MacBook (unknown MacOS X version).

Always with the Oracle JDK6. Never tested with other VM vendors.

String findDnsSuffix() {

// First I get the hosts name
// This one never contains the DNS suffix (Don't know if that is the case for all VM vendors)
String hostName = InetAddress.getLocalHost().getHostName().toLowerCase();

// Alsways convert host names to lower case. Host names are 
// case insensitive and I want to simplify comparison.

// Then I iterate over all network adapters that might be of interest
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();

if (ifs == null) return ""; // Might be null

for (NetworkInterface iF : Collections.list(ifs)) { // convert enumeration to list.
    if (!iF.isUp()) continue;

    for (InetAddress address : Collections.list(iF.getInetAddresses())) {
        if (address.isMulticastAddress()) continue;

        // This name typically contains the DNS suffix. Again, at least on Oracle JDK
        String name = address.getHostName().toLowerCase();

        if (name.startsWith(hostName)) {
            String dnsSuffix = name.substring(hostName.length());
            if (dnsSuffix.startsWith(".")) return dnsSuffix;
        }
    }
}

return "";
}

Note: I wrote the code in the editor, did not copy the solution actually used. It also contains no error handling, like computers without a name, failure to resolve a DNS name, ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文