4

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"?

arjunaw
  • 101
  • 1
  • 5

2 Answers2

3

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:
arjunaw
  • 101
  • 1
  • 5
1

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, ...

AndiHofi
  • 39
  • 4
  • This is rather late, but can you explain what exactly the outcome is for you on Windows 10. A very similar code works in production for years and it always worked. Also on Windows 10. There may be bugs though. – AndiHofi Oct 22 '18 at 08:10