I am writing a program for windows which lists all network adapters and deactivates all except the one selected. I know how to modify network adapters with netsh, but the problem is I can't get the relevant information from the NetworkInterface class.
The NetworkInterface class has the methods getName() and getDisplayName(), but they seem to produce only irrelevant data.
I took the example directly from oracle:
http://docs.oracle.com/javase/tutorial/networking/nifs/listing.html
public class ListNets {
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
}
This example produces this kind of output:
Display name: Intel(R) Ethernet Connection (2) I219-V Name: eth4 InetAddress: /10.0.0.4 InetAddress: /fe80:0:0:0:cc75:ed6:3089:bd61%eth4
But I can't use this information, because netsh requires the name of the adapter, which is Ethernet 1
not Intel(R) Ethernet Connection (2) I219-V
or eth4
.
In my opinion getName() should return a OS specific name with usage in the OS itself. Is there any possibility to retrive the real name of the adapter with other standard java classes?
At the moment my only solution would involve getmac /fo csv /v
to get the right information and parse it. Due to the fact that i don't want to write my own wrapper which accesses the c++ function GetAdaptersAddresses
, because i would need to learn it.