2009-08-26 88 views
4

我知道你可以用Java 6做到这一点,使用java.net.NetworkInterface->getHardwareAddress()。但我部署的环境仅限于Java 5.如何使用Java 5获取主机mac地址?

有没有人知道如何在Java 5或更早版本中执行此操作?非常感谢。

+0

不是一个答案(因此评论),但如果没有内置的方式,您可以随时从Java 6中获取代码并将其返回。源可用。 – 2009-08-26 09:08:10

+1

这是Java 6的本地调用,所以backport不起作用。在没有连接的情况下获取Mac地址可能毫无意义。在某些情况下,您可能会收到虚拟设备使用的假Mac地址。这些地址不是全球唯一的。已知几种VPN驱动程序,AOL客户端可以在Windows上安装带有固定Mac的设备。他们很少弹出到第一个设备,但它发生在我们以前。 – 2009-08-26 10:12:43

+0

当主机有8个MAC地址时,你想要哪个MAC地址? – 2009-09-03 14:03:06

回答

6

Java 5中的标准方式是启动本机进程来运行ipconfigifconfig并解析OutputStream以获得答案。

例如:

private String getMacAddress() throws IOException { 
    String command = “ipconfig /all”; 
    Process pid = Runtime.getRuntime().exec(command); 
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream())); 
    Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”); 
    while (true) { 
     String line = in.readLine(); 
     if (line == null) 
      break; 
     Matcher m = p.matcher(line); 
     if (m.matches()) { 
      return m.group(1); 
     } 
    } 
} 
+0

这只适用于Windows。有没有更好的解决方案获得操作系统无关的MAC地址? – 2012-05-14 09:51:01

+0

任何人都可以帮助修改此代码使其与操作系统无关? – Sumit 2017-09-27 11:24:06

1

据我知道有没有纯Java的前6的解决方案。 UUID解决了这个问题,但是首先找到它是否应该运行ifconfig或ipconfig。

3

Butterchicken的解决方案是可以的,但只适用于英文版的Windows。

一个稍微好一些(独立于语言)的解决方案将匹配MAC地址的模式。在这里,我还要确保该地址有关联的IP(例如过滤掉的蓝牙设备):

public String obtainMacAddress() 
throws Exception { 
    Process aProc = Runtime.getRuntime().exec("ipconfig /all"); 
    InputStream procOut = new DataInputStream(aProc.getInputStream()); 
    BufferedReader br = new BufferedReader(new InputStreamReader(procOut)); 

    String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})"; 
    Pattern aPatternMac = Pattern.compile(aMacAddress); 
    String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$"; 
    Pattern aPatternIp = Pattern.compile(aIpAddress); 
    String aNewAdaptor = "[A-Z].*$"; 
    Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor); 

    // locate first MAC address that has IP address 
    boolean zFoundMac = false; 
    boolean zFoundIp = false; 
    String foundMac = null; 
    String theGoodMac = null; 

    String strLine; 
    while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) { 
     Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine); 
     if (aMatcherNewAdaptor.matches()) { 
      zFoundMac = zFoundIp = false; 
     } 
     Matcher aMatcherMac = aPatternMac.matcher(strLine); 
     if (aMatcherMac.find()) { 
      foundMac = aMatcherMac.group(0); 
      zFoundMac = true; 
     } 
     Matcher aMatcherIp = aPatternIp.matcher(strLine); 
     if (aMatcherIp.matches()) { 
      zFoundIp = true; 
      if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac; 
     } 
    } 

    aProc.destroy(); 
    aProc.waitFor(); 

    return theGoodMac; 
} 
+0

这只适用于Windows。有没有更好的解决方案获得操作系统无关的MAC地址? – 2012-05-14 09:49:58

+0

不在Java5中,除非您重写此方法以在例如Unix并调用基于系统属性“os.name”的一个或另一个。在Java6中,你可以。但问题是关于Java5 – 2012-05-14 12:46:56

+0

是的,因此我正在迭代。此解决方案仅适用于Windows中的JDK 5,但不适用于其他操作系统。 – 2012-05-14 16:03:42

1

在Linux和Mac OS X光机,你可能需要使用ifconfig -a
ipconfig是windows命令。