As part of my Windows exclusive Java application, I need to query various hardware properties - CPU/BIOS/Hard disk serial numbers, and MAC addresses.
So I can use WMIC and other Windows specific tools via a Runtime.exec
call.
Querying the CPU and BIOS serial numbers is easy; for example,
wmic BIOS get SerialNumber
does the trick.
I haven't found a similar simple solution to list out MAC addresses.
wmic nic list brief
will show me a badly formatted list, but then parsing the output with regex is a pain.
Looking for WMIC related methods all point to some variation of the following VBScript:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
if objItem.IPEnabled = 0 And objItem.ServiceName <> "VMnetAdapter" And isNull(objItem.MACAddress) = 0 Then
Wscript.Echo objItem.MACAddress
End if
Next
Storing and running the VBS is a security hazard, so what's currently being done is to write out the VBscript to a text file, execute, then delete it. This is messy and slow.I have used wmic directly for querying other system properties, such as the BIOS serial number.
A pure Java solution would be much better, but I've not come across any for all available MAC addresses. The closest I've seen is this, for IP addresses.
Is there a way to do this from Java? I should add that I'm restricted to using Java 6, though I hear Java 7 has a lot more useful networking APIs.
Update : Forgot to add that NetworkInterface
only returns me the IPv4 Ethernet MAC, none of the other virtual adapters.