| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 662 人关注过本帖
标题:java 不同操作系统获取MAC地址方法详解
只看楼主 加入收藏
xioun
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:68
专家分:309
注 册:2013-4-8
结帖率:85.71%
收藏
 问题点数:0 回复次数:1 
java 不同操作系统获取MAC地址方法详解
public class ReadMacAdrss
{
  public static final boolean isWindows = System.getProperty("os.name")
    .startsWith("Win");

  public static final boolean isLinux = System.getProperty("os.name").equals(
    "Linux");

  public static final boolean isAIX = System.getProperty("os.name").equals(
    "AIX");

  public static final boolean isSolaris = (System.getProperty("os.name")
    .equals("Solaris")) ||
    (System.getProperty("os.name").equals("SunOS"));

  public static final boolean isHPUX = System.getProperty("os.name").equals(
    "HP-UX");
  public static final String WindowsCmd = "cmd.exe /c ipconfig/all";
  public static final String LinuxCmd = "ifconfig -a";
  public static final String AIXCmd = "netstat -ia";
  public static final String SolarisCmd = "/sbin/ifconfig -a";
  public static final String HPUXCmd = "/usr/sbin/lanscan";
  private String mPhysicalAddress = "";

  private int mPhysicalMacNumber = 0;

  private boolean isInit = false;

  public void init()
  {
    try
    {
      Process process = Runtime.getRuntime().exec(getMacCommand());
      BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));
      Pattern macPattern = (getRegStr());
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        if ("".equals(line))
          continue;
        Matcher macMatcher = macPattern.matcher(line);
        boolean result = macMatcher.find();
        if (result) {
          this.mPhysicalMacNumber += 1;
          String mac = macMatcher.group(0);
          mac = mac.replace(':', '-');
          if (isHPUX)
          {
            mac = dealMac(mac);
          }
          if ("".equals(this.mPhysicalAddress)) {
            this.mPhysicalAddress = mac;
          }
          else
          {
            ReadMac tmp139_138 = this; tmp139_138.mPhysicalAddress = tmp139_138.mPhysicalAddress + "," + mac;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    this.isInit = true;
  }

  public String getMacCommand()
  {
    if (isWindows) {
      return "cmd.exe /c ipconfig/all";
    }
    if (isLinux) {
      return "ifconfig -a";
    }
    if (isAIX) {
      return "netstat -ia";
    }
    if (isSolaris) {
      return "/sbin/ifconfig -a";
    }
    if (isHPUX) {
      return "/usr/sbin/lanscan";
    }
    return "";
  }

  public String getRegStr()
  {
    if (isWindows) {
      return "([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}";
    }
    if (isLinux) {
      return "([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}";
    }
    if (isAIX) {
      return "([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}";
    }
    if (isSolaris) {
      return "([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}";
    }
    if (isHPUX)
    {
      return "0x[0-9a-zA-Z]{12}";
    }
    return "";
  }

  public String getPhysicalAddress()
  {
    if (this.isInit) {
      return this.mPhysicalAddress;
    }
    return "Mac is not init.";
  }

  public int getPhysicalMacNumber() {
    if (this.isInit) {
      return this.mPhysicalMacNumber;
    }
    System.out.println("Mac is not init.");
    return 0;
  }

  public String dealMac(String mac)
  {
    mac = mac.substring(2);
    StringBuffer sAdd = new StringBuffer();
    for (int i = 0; i < 12; ++i) {
      sAdd.append(mac.charAt(i));
      if (i % 2 == 1) {
        sAdd.append("-");
      }
    }
    return sAdd.substring(0, sAdd.length() - 1);
  }
搜索更多相关主题的帖子: static 操作系统 public equals Linux 
2013-04-24 09:05
xioun
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:68
专家分:309
注 册:2013-4-8
收藏
得分:0 
是否还有更好的想法,各位多多指教!

命若天定,我就破了这个天!
2013-04-26 08:59
快速回复:java 不同操作系统获取MAC地址方法详解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.096147 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved