2013-09-26 30 views
0

我使用的是使用Java API的非常simple code显示网络信息:NetworkInterface#getHardwareAddress()Windows 7上的配置是否会对Java中的NetworkInterface.getHardwareAddress()产生影响?

该代码正在使用Windows XP,XP 64,Debian。

在Win 7上我发现了两种不同的行为:我的公司和我的公司的计算机。 显示的信息与ipconfig /all不一样,我只得到最后一个虚拟网卡的物理地址。

我使用java 1.6 u32,1.7 u21和1.7 u40(两个版本x86/64)重现了该问题:查看output,eth3和eth4返回错误的mac地址。

我认为代码是正确的:这与在stackoverflow上建议的一样,并且结果在我的个人计算机上是正确的。

  • 有谁知道哪些参数可能会影响结果?
  • 我应该在Windows上检查哪些设置以确定 不同机器之间的差异?
  • 有什么建议吗?

待办事项

我会尝试禁用虚拟接口然后重新启动该工具。 (需要IT干预...)。

+0

代码如何?你是否将字节转换为整数以将mac显示为字符串? – cheffe

+0

可能的重复 http://stackoverflow.com/questions/6164167/get-mac-address-on-local-machine-with-java –

+0

@AbhijithNagarajan,我知道如何检索网络信息。我的代码正在使用Windows XP或Debian。主题是这个Java API不提供Windows 7上的正确信息。 – chepseskaf

回答

0

我有同样的问题。这里是我的Windows 7机器上使用VMVare虚拟卡的代码:

private String getMacJava5() throws Exception { 
    String mac = ""; 

    InetAddress ip = InetAddress.getLocalHost(); 

    String[] command = {"ipconfig", "/all"}; 
    Pattern physAddr = Pattern.compile("\\s*Physi.*: (.*)"); 
    Pattern ipAddr = Pattern.compile("\\s*IPv.*: ([^(]*).*"); 

    Process p = Runtime.getRuntime().exec(command); 
    BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    while (true) { 
     String line = inn.readLine(); 
     if (line == null) { 
      break; 
     } 

     Matcher mm = physAddr.matcher(line); 
     if (mm.matches()) { 
      mac = mm.group(1); 
     } 
     mm = ipAddr.matcher(line); 
     if (mm.matches()) { 
      if (mm.group(1).equals(ip.getHostAddress())) { 
       break; 
      } 
      mac = ""; 
     } 
    } 
    return mac + " IP: " + ip.getHostAddress(); 
} 
+0

我试过这个解决方案,但它不是跨平台的。那么Linux或OSX呢? ifconfig是否授权给非sudoers? – chepseskaf

相关问题