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 = Pattern.compile(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);
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值