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"?
Ok so I figured out how to do this on Windows XP and Windows 7:
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, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With