2011-05-26 18 views
2

在Java程序中是否可以访问Windows计算机的ipconfig/all输出的“连接特定的DNS后缀”字段中包含的字符串?如何访问Java中每个NetworkInterface的连接特定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()将获得返回描述(例如:博通NetXtreme千兆以太网),并且getInetAddresses()会给我一个绑定到这个网络接口的IP地址列表。

但也有阅读的“连接特定的DNS后缀”的方式?

回答

3

好了,所以我想通了,如何做到这一点在Windows XP和Windows 7:包含在每个网络 的连接特定的 DNS后缀领域:(myexample.com如)

  • 字符串在 输出中列出接口IPCONFIG/ALL可以在 注册表 HKEY_LOCAL_MACHINE \ SYSTEM找到\ CURRENTCONTROLSET \服务\ TCPIP \参数\接口{GUID} (其中GUID是感兴趣的 网络接口的GUID)作为 圣环值(类型REG_SZ)名为DhcpDomain。
  • 访问Windows注册表项不是直接在Java中,但通过一些巧妙地利用反射的就可以访问下HKEY_LOCAL_MACHINE \ SYSTEM发现\ CURRENTCONTROLSET所需的网络适配器的关键\服务\ TCPIP \参数\接口\,然后读取字符串数据元素名称为DhcpDomain;它的值是必需的字符串。
  • 参见从Java访问Windows注册表 的例子 以下链接:
0

我用一个更复杂的方法,这在所有平台上的工作原理。

测试在Windows 7,Ubuntu的12.04和一些未知的Linux发行版(詹金斯建立主机)和一个的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名称,...

+0

这并不为我工作(Java 8中,Windows 10)。 – jumar 2016-10-17 14:12:47