[转帖]java(jdk1.4、jdk1.6)获取mac地址的方法(winxp、win2003、win7、linux)_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3870 | 回复: 0   主题: [转帖]java(jdk1.4、jdk1.6)获取mac地址的方法(winxp、win2003、win7、linux)        下一篇 
q563725885
注册用户
等级:少校
经验:1134
发帖:98
精华:1
注册:2013-5-7
状态:离线
发送短消息息给q563725885 加好友    发送短消息息给q563725885 发消息
发表于: IP:您无权察看 2013-5-13 15:39:02 | [全部帖] [楼主帖] 楼主

项目是年代久远的了,用的是jdk1.4,登录验证需要加mac地址验证,而且是需要兼容主流平台的操作系统。

得,任务落到了咱的头上,啃之。

考虑到jdk1.4没有API取得mac地址,故使用系统命令cmd ipconfig /all  截取mac地址。但考虑到环境变量的改变有可能影响到 ipconfig命令的执行,而ipconfig.exe命令在操作

系统的路径是c:\windows\system32\ipconfig.我们只要获取到环境变量中windir的值,即c:\window

贴上该类,jdk1.4下,在winxp(包括虚拟机)、win2003(包括虚拟机)、win7(联想)均通过,linux和unix没测试。如有错误,欢迎指正。

[java]view plaincopyprint?

  1. /** 
  2.  * 
  3.  * @author hyq 
  4.  * jdk1.4下获取mac地址 
  5.  */ 
  6. public class GetMacAddress { 
  7.       
  8.        /** 
  9.        * 获取当前操作系统名称. return 操作系统名称 例如:windows,Linux,Unix等. 
  10.        */ 
  11.        public static String getOSName() { 
  12.              return System.getProperty("os.name").toLowerCase(); 
  13.        } 
  14.       
  15.        /** 
  16.        * 获取Unix网卡的mac地址. 
  17.        * @return mac地址 
  18.        */ 
  19.        public static String getUnixMACAddress() { 
  20.              String mac = null; 
  21.              BufferedReader bufferedReader = null; 
  22.              Process process = null; 
  23.              try { 
  24.                    // Unix下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息 
  25.                    process = Runtime.getRuntime().exec("ifconfig eth0"); 
  26.                    bufferedReader = new BufferedReader(new InputStreamReader(process 
  27.                    .getInputStream())); 
  28.                    String line = null; 
  29.                    int index = -1; 
  30.                    while ((line = bufferedReader.readLine()) != null) { 
  31.                          // 寻找标示字符串[hwaddr] 
  32.                          index = line.toLowerCase().indexOf("hwaddr"); 
  33.                          if (index != -1) { 
  34.                                // 取出mac地址并去除2边空格 
  35.                                mac = line.substring(index + "hwaddr".length() + 1).trim(); 
  36.                                break; 
  37.                          } 
  38.                    } 
  39.              } catch (IOException e) { 
  40.                    e.printStackTrace(); 
  41.              } finally { 
  42.              try { 
  43.                    if (bufferedReader != null) { 
  44.                          bufferedReader.close(); 
  45.                    } 
  46.              } catch (IOException e1) { 
  47.                    e1.printStackTrace(); 
  48.              } 
  49.              bufferedReader = null; 
  50.              process = null; 
  51.        } 
  52.       
  53.        return mac; 
  54.  } 
  55.  /** 
  56.  * 获取Linux网卡的mac地址 
  57.  * @return mac地址 
  58.  */ 
  59.  public static String getLinuxMACAddress() { 
  60.        String mac = null; 
  61.        BufferedReader bufferedReader = null; 
  62.        Process process = null; 
  63.        try { 
  64.              // linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息 
  65.              process = Runtime.getRuntime().exec("ifconfig eth0"); 
  66.              bufferedReader = new BufferedReader(new InputStreamReader(process 
  67.              .getInputStream())); 
  68.              String line = null; 
  69.              int index = -1; 
  70.              while ((line = bufferedReader.readLine()) != null) { 
  71.                    index = line.toLowerCase().indexOf("硬件地址"); 
  72.                    if (index != -1) { 
  73.                          // 取出mac地址并去除2边空格 
  74.                          mac = line.substring(index + 4).trim(); 
  75.                          break; 
  76.                    } 
  77.              } 
  78.        } catch (IOException e) { 
  79.              e.printStackTrace(); 
  80.        } finally { 
  81.        try { 
  82.              if (bufferedReader != null) { 
  83.                    bufferedReader.close(); 
  84.              } 
  85.        } catch (IOException e1) { 
  86.              e1.printStackTrace(); 
  87.        } 
  88.        bufferedReader = null; 
  89.        process = null; 
  90.  } 
  91.  return mac; 
  92.  } 
  93.  /** 
  94.  * 获取widnowXp网卡的mac地址. 
  95.  * @return mac地址 
  96.  */ 
  97.  public static String getWindowXPMACAddress(String execStr) { 
  98.        String mac = null; 
  99.        BufferedReader bufferedReader = null; 
  100.        Process process = null; 
  101.        try { 
  102.             
  103.              // windows下的命令,显示信息中包含有mac地址信息 
  104.              process = Runtime.getRuntime().exec(execStr); 
  105.              bufferedReader = new BufferedReader(new InputStreamReader(process 
  106.              .getInputStream())); 
  107.              String line = null; 
  108.              int index = -1; 
  109.              while ((line = bufferedReader.readLine()) != null) { 
  110.                    if(line.indexOf("本地连接") != -1) //排除有虚拟网卡的情况 
  111.                    continue; 
  112.                   
  113.                    // 寻找标示字符串[physical address] 
  114.                    index = line.toLowerCase().indexOf("physical address"); 
  115.                    if (index != -1) { 
  116.                          index = line.indexOf(":"); 
  117.                          if (index != -1) { 
  118.                                //取出mac地址并去除2边空格 
  119.                                mac = line.substring(index + 1).trim(); 
  120.                          } 
  121.                          break; 
  122.                    } 
  123.                   
  124.              } 
  125.        } catch (IOException e) { 
  126.              e.printStackTrace(); 
  127.        } finally { 
  128.        try { 
  129.              if (bufferedReader != null) { 
  130.                    bufferedReader.close(); 
  131.              } 
  132.        } catch (IOException e1) { 
  133.              e1.printStackTrace(); 
  134.        } 
  135.        bufferedReader = null; 
  136.        process = null; 
  137.  } 
  138.  return mac; 
  139.  } 
  140.  /** 
  141.  * 获取widnow7网卡的mac地址. 
  142.  * @return mac地址 
  143.  */ 
  144.  public static String getWindow7MACAddress(String execStr) { 
  145.        String mac = null; 
  146.        BufferedReader bufferedReader = null; 
  147.        Process process = null; 
  148.        try { 
  149.             
  150.              process = Runtime.getRuntime().exec(execStr); 
  151.              bufferedReader = new BufferedReader(new InputStreamReader(process 
  152.              .getInputStream())); 
  153.              String line = null; 
  154.              int index = -1; 
  155.              while ((line = bufferedReader.readLine()) != null) { 
  156.                    index = line.toLowerCase().indexOf("物理地址"); 
  157.                    if (index != -1) { 
  158.                          index = line.indexOf(":"); 
  159.                          if (index != -1) { 
  160.                                mac = line.substring(index + 1).trim(); 
  161.                          } 
  162.                          break; 
  163.                    } 
  164.                   
  165.              } 
  166.        } catch (IOException e) { 
  167.              e.printStackTrace(); 
  168.        } finally { 
  169.        try { 
  170.              if (bufferedReader != null) { 
  171.                    bufferedReader.close(); 
  172.              } 
  173.        } catch (IOException e1) { 
  174.              e1.printStackTrace(); 
  175.        } 
  176.        bufferedReader = null; 
  177.        process = null; 
  178.  } 
  179.  return mac; 
  180.  } 
  181.  /** 
  182.  * 获取MAC地址 
  183.  */ 
  184.  public static String getMacAddress(){ 
  185.        String os = getOSName(); 
  186.        String execStr = getSystemRoot()+"/system32/ipconfig /all"; 
  187.        String mac = null; 
  188.        if (os.startsWith("windows")) { 
  189.              if(os.equals("windows xp")){ //xp 
  190.                    mac = getWindowXPMACAddress(execStr); 
  191.              }else if(os.equals("windows 2003")){ //2003 
  192.                    mac = getWindowXPMACAddress(execStr); 
  193.              }else{ //win7 
  194.              mac = getWindow7MACAddress(execStr); //B8-70-F4-49-2B-EE 
  195.        } 
  196.       
  197.  } else if (os.startsWith("linux")) { 
  198.        mac = getLinuxMACAddress(); 
  199.  } else { 
  200.  mac = getUnixMACAddress(); 
  201.  } 
  202.  return mac; 
  203.  } 
  204.  /** 
  205.  * jdk1.4获取系统命令路径 :SystemRoot=C:\WINDOWS 
  206.  * @return 
  207.  */ 
  208.  public static String getSystemRoot(){ 
  209.        String cmd = null; 
  210.        String os = null; 
  211.        String result = null; 
  212.        String envName = "windir"; 
  213.       
  214.        os = System.getProperty("os.name").toLowerCase(); 
  215.       
  216.        if (os.startsWith("windows")) { 
  217.              cmd = "cmd /c SET"; 
  218.        } else { 
  219.        cmd = "env"; 
  220.  } 
  221.  try { 
  222.        Process p = Runtime.getRuntime().exec(cmd); 
  223.        InputStreamReader isr = new InputStreamReader(p.getInputStream()); 
  224.        BufferedReader commandResult = new BufferedReader(isr); 
  225.        String line = null; 
  226.        while ((line = commandResult.readLine()) != null) { 
  227.              if (line.indexOf(envName) > -1) { 
  228.                    result = line.substring(line.indexOf(envName) 
  229.                    + envName.length() + 1); 
  230.                    return result; 
  231.              } 
  232.        } 
  233.  } catch (Exception e) { 
  234.        System.out.println("获取系统命令路径 error: " + cmd + ":" + e); 
  235.  } 
  236.  return null; 
  237.  } 
  238.  public static void main(String[] args) { 
  239.        System.out.println("mac地址为: "+getMacAddress()); 
  240.  } 


/**
*
* @author hyq
* jdk1.4下获取mac地址
*/
public class GetMacAddress {

       /**
       * 获取当前操作系统名称. return 操作系统名称 例如:windows,Linux,Unix等.
       */
       public static String getOSName() {
             return System.getProperty("os.name").toLowerCase();
       }

       /**
       * 获取Unix网卡的mac地址.
       * @return mac地址
       */
       public static String getUnixMACAddress() {
             String mac = null;
             BufferedReader bufferedReader = null;
             Process process = null;
             try {
                   // Unix下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
                   process = Runtime.getRuntime().exec("ifconfig eth0");
                   bufferedReader = new BufferedReader(new InputStreamReader(process
                   .getInputStream()));
                   String line = null;
                   int index = -1;
                   while ((line = bufferedReader.readLine()) != null) {
                         // 寻找标示字符串[hwaddr]
                         index = line.toLowerCase().indexOf("hwaddr");
                         if (index != -1) {
                               // 取出mac地址并去除2边空格
                               mac = line.substring(index + "hwaddr".length() + 1).trim();
                               break;
                         }
                   }
             } catch (IOException e) {
             e.printStackTrace();
       } finally {
       try {
             if (bufferedReader != null) {
                   bufferedReader.close();
             }
       } catch (IOException e1) {
       e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 获取Linux网卡的mac地址
* @return mac地址
*/
public static String getLinuxMACAddress() {
       String mac = null;
       BufferedReader bufferedReader = null;
       Process process = null;
       try {
             // linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
             process = Runtime.getRuntime().exec("ifconfig eth0");
             bufferedReader = new BufferedReader(new InputStreamReader(process
             .getInputStream()));
             String line = null;
             int index = -1;
             while ((line = bufferedReader.readLine()) != null) {
                   index = line.toLowerCase().indexOf("硬件地址");
                   if (index != -1) {
                         // 取出mac地址并去除2边空格
                         mac = line.substring(index + 4).trim();
                         break;
                   }
             }
       } catch (IOException e) {
       e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
       bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 获取widnowXp网卡的mac地址.
* @return mac地址
*/
public static String getWindowXPMACAddress(String execStr) {
       String mac = null;
       BufferedReader bufferedReader = null;
       Process process = null;
       try {

             // windows下的命令,显示信息中包含有mac地址信息
             process = Runtime.getRuntime().exec(execStr);
             bufferedReader = new BufferedReader(new InputStreamReader(process
             .getInputStream()));
             String line = null;
             int index = -1;
             while ((line = bufferedReader.readLine()) != null) {
                   if(line.indexOf("本地连接") != -1) //排除有虚拟网卡的情况
                   continue;

                   // 寻找标示字符串[physical address]
                   index = line.toLowerCase().indexOf("physical address");
                   if (index != -1) {
                         index = line.indexOf(":");
                         if (index != -1) {
                               //取出mac地址并去除2边空格
                               mac = line.substring(index + 1).trim();
                         }
                         break;
                   }

             }
       } catch (IOException e) {
       e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
       bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 获取widnow7网卡的mac地址.
* @return mac地址
*/
public static String getWindow7MACAddress(String execStr) {
       String mac = null;
       BufferedReader bufferedReader = null;
       Process process = null;
       try {

             process = Runtime.getRuntime().exec(execStr);
             bufferedReader = new BufferedReader(new InputStreamReader(process
             .getInputStream()));
             String line = null;
             int index = -1;
             while ((line = bufferedReader.readLine()) != null) {
                   index = line.toLowerCase().indexOf("物理地址");
                   if (index != -1) {
                         index = line.indexOf(":");
                         if (index != -1) {
                               mac = line.substring(index + 1).trim();
                         }
                         break;
                   }

             }
       } catch (IOException e) {
       e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
       bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 获取MAC地址
*/
public static String getMacAddress(){
       String os = getOSName();
       String execStr = getSystemRoot()+"/system32/ipconfig /all";
       String mac = null;
       if (os.startsWith("windows")) {
             if(os.equals("windows xp")){ //xp
                   mac = getWindowXPMACAddress(execStr);
             }else if(os.equals("windows 2003")){ //2003
                   mac = getWindowXPMACAddress(execStr);
             }else{ //win7
             mac = getWindow7MACAddress(execStr); //B8-70-F4-49-2B-EE
       }

} else if (os.startsWith("linux")) {
       mac = getLinuxMACAddress();
} else {
mac = getUnixMACAddress();
}
return mac;
}

/**
* jdk1.4获取系统命令路径 :SystemRoot=C:\WINDOWS
* @return
*/
public static String getSystemRoot(){
       String cmd = null;
       String os = null;
       String result = null;
       String envName = "windir";

       os = System.getProperty("os.name").toLowerCase();

       if (os.startsWith("windows")) {
             cmd = "cmd /c SET";
       } else {
       cmd = "env";
}

try {
Process p = Runtime.getRuntime().exec(cmd);
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader commandResult = new BufferedReader(isr);
String line = null;
while ((line = commandResult.readLine()) != null) {
       if (line.indexOf(envName) > -1) {
             result = line.substring(line.indexOf(envName)
             + envName.length() + 1);
             return result;
       }
}
} catch (Exception e) {
System.out.println("获取系统命令路径 error: " + cmd + ":" + e);
}
return null;
}

public static void main(String[] args) {
       System.out.println("mac地址为: "+getMacAddress());
}

}

;

再附上一个jdk1.6获取mac的方法,但这个方法没进行过不同操作系统的测试。

[java]view plaincopyprint?

  1. /** 
  2.  * jdk1.6下API获取mac地址 
  3.  * @return 
  4.  */ 
  5.  public static String getMAC() { 
  6.        Enumeration<NetworkInterface> el; 
  7.        String mac_s = ""; 
  8.        try { 
  9.              el = NetworkInterface.getNetworkInterfaces(); 
  10.              while (el.hasMoreElements()) { 
  11.                    byte[] mac = el.nextElement().getHardwareAddress(); 
  12.                    if (mac == null) 
  13.                    continue; 
  14.                    mac_s = hexByte(mac[0]) + "-" + hexByte(mac[1]) + "-" 
  15.                    + hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-" 
  16.                    + hexByte(mac[4]) + "-" + hexByte(mac[5]); 
  17.                    System.out.println(mac_s); 
  18.                   
  19.              } 
  20.        } catch (SocketException e1) { 
  21.              e1.printStackTrace(); 
  22.        } 
  23.        return mac_s; 
  24.  } 


/**
* jdk1.6下API获取mac地址
* @return
*/
public static String getMAC() {
       Enumeration<NetworkInterface> el;
       String mac_s = "";
       try {
             el = NetworkInterface.getNetworkInterfaces();
             while (el.hasMoreElements()) {
                   byte[] mac = el.nextElement().getHardwareAddress();
                   if (mac == null)
                   continue;
                   mac_s = hexByte(mac[0]) + "-" + hexByte(mac[1]) + "-"
                   + hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-"
                   + hexByte(mac[4]) + "-" + hexByte(mac[5]);
                   System.out.println(mac_s);

             }
       } catch (SocketException e1) {
       e1.printStackTrace();
}
return mac_s;
}

;




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论