Reputation: 2184
How can I obtain the media state of a network adapter in windows? I've searched a bit and it doesn't look like the java.net.NetworkInterface class provides this ability, which makes sense as it appears to be a windows concept that doesn't even apply to all adapters. When I run ipconfig I get something like the following:
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection 5:
Connection-specific DNS Suffix . : BlahBlah.Blah
IPv4 Address. . . . . . . . . . . : 192.168.113.44
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.113.1
Ethernet adapter Local Area Connection 8:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : BlahBlah.Blah
And if I use the script found at Can anyone explain why Java GetNetworkInterfaces returns so many interfaces on Windows 7 to obtain details from these adapters (with the others removed) I get something like the following:
Display name: Software Loopback Interface 1
Name: lo
InetAddress: /0:0:0:0:0:0:0:1
InetAddress: /127.0.0.1
Up? true
Loopback? true
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: []
MTU: -1
Display name: Intel(R) 82579LM Gigabit Network Connection #2
Name: eth13
InetAddress: /172.16.4.29
Up? false
Loopback? false
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: [60, -105, 14, -77, -123, 110]
MTU: 1500
Display name: Intel(R) Centrino(R) Advanced-N 6205 #3
Name: net11
InetAddress: /192.168.113.44
Up? false
Loopback? false
PointToPoint? false
Supports multicast? true
Virtual? false
Hardware address: [60, -105, 14, -77, -123, 110]
MTU: 1500
Thoughts on how I can determine if the ethernet adapter is "Media disconnected"?
Upvotes: 3
Views: 1109
Reputation: 53
You can take reference of the below snippet. Basically !networkInterface.isUp()
should do the trick for you.
List<NetworkInterface> nic = new ArrayList<>();
interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (!networkInterface.isUp())
continue;
nic.add(networkinterface);
}
Upvotes: 1