0

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.

Community
  • 1
  • 1
Rex
  • 801
  • 1
  • 17
  • 26

3 Answers3

1

I get the MAC addresses for VPN and other interfaces from java.net.NetworkInterface. For all the ones listed with a "Physical Address" using the command: ipconfig /all at least, doubt we can do better than that?

public static void main(String[] args) {
    printNetworkInterfaces(NetworkInterface.getNetworkInterfaces(), 1);
}

private static void printNetworkInterfaces(Enumeration<NetworkInterface> netIfs, int indent) {
    for (NetworkInterface netIf : Collections.list(netIfs)) {
        System.out.printf("%" + indent + "s%s (%s): %s%n", "", netIf.getName(), netIf.getDisplayName(), formatHwAddress(netIf));
        printNetworkInterfaces(netIf.getSubInterfaces(), indent + 2);
    }
}

....

Extract from the output:

eth4 (Intel(R) 82579V Gigabit Network Connection): xx:xx:xx:xx:xx:xx <- a real MAC address here
net4 (Teredo Tunneling Pseudo-Interface): 00:00:00:00:00:00:00:e0
net5 (Microsoft ISATAP Adapter): 00:00:00:00:00:00:00:e0
eth12 (TAP-Win32 Adapter V9): xx:xx:xx:xx:xx:xx <- a real MAC address here

This was using java 7u3 though, might be different on java 6.

Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • I've tried a variant of this, it only returns me IPv4 interface MACs. I need both IPv4 and IPv6. – Rex Mar 05 '12 at 05:05
  • Are you saying that you have physical NICs with only IPv6 set up for them and you don't get their physical address/MAC? Normally you have both IPv4 and IPv6 assigned to one physical NIC and then you only have one physical address/MAC for both of them. – Mattias Isegran Bergander Mar 05 '12 at 07:53
  • I tried printing physical address/MAC along with the IP type and IP and it works for mer at least, example: `eth3 Physical Address: 00:23:54:48:44:ab Addresses: [IPv6: fe80:0:0:0:9000:fca:6bab:f0e0%10]` and `net3 (Microsoft ISATAP Adapter) Physical Address: 00:00:00:00:00:00:00:e0 Addresses: [IPv6: fe80:0:0:0:0:5efe:c0a8:407%11]` and `eth4 Physical Address: 00:23:54:48:44:aa Addresses: [IPv4: 192.168.4.7, IPv6: fe80:0:0:0:9cd0:3169:4db4:c3b5%12]` – Mattias Isegran Bergander Mar 05 '12 at 11:25
1

What don't you just try to access WMI from java ? perhaps with jWMI – Query Windows WMI from Java

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • This is the same as what I was trying to do initially - write out a temporary VB script file and run it..but looks like the only solution for now. – Rex Mar 07 '12 at 08:38
0

You can get MAC address in Java 1.6 via java.net.NetworkInterface.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • It only returns the ethernet MAC. I need it for the VPN & other interfaces also. – Rex Mar 01 '12 at 06:04