当前位置: 代码迷 >> Java相关 >> java 不同操作系统获取MAC地址方法详解
  详细解决方案

java 不同操作系统获取MAC地址方法详解

热度:374   发布时间:2013-04-24 09:05:19.0
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);
  }
搜索更多相关的解决方案: static  操作系统  public  equals  Linux  

----------------解决方案--------------------------------------------------------
是否还有更好的想法,各位多多指教!
----------------解决方案--------------------------------------------------------
  相关解决方案