分享

JAVA获得MAC地址

 再生资源保存站 2014-07-08
  • package com.etong.bms; 
  •   
  • import java.io.BufferedInputStream;  
  • import java.io.IOException;  
  • import java.io.InputStream;  
  • import java.net.InetAddress;  
  • import java.util.StringTokenizer;  
  •   
  • import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;  
  •   
  • public final class GetMacAddr {  
  •     private final static String getMacAddress() throws IOException {  
  •         String os = System.getProperty("os.name");  
  •   
  •         try {  
  •             if (os.startsWith("Windows")) {  
  •                 return windowsParseMacAddress(windowsRunIpConfigCommand());  
  •             } else if (os.startsWith("Linux")) {  
  •                 return linuxParseMacAddress(linuxRunIfConfigCommand());  
  •             } else {  
  •                 throw new IOException("unknown operating system: " + os);  
  •             }  
  •         } catch (ParseException ex) {  
  •             ex.printStackTrace();  
  •             throw new IOException(ex.getMessage());  
  •         }  
  •     }  
  •   
  •     /* 
  •      * Linux stuff 
  •      */  
  •     private final static String linuxParseMacAddress(String ipConfigResponse)  
  •             throws ParseException {  
  •         String localHost = null;  
  •         try {  
  •             localHost = InetAddress.getLocalHost().getHostAddress();  
  •         } catch (java.net.UnknownHostException ex) {  
  •             ex.printStackTrace();  
  •             throw new ParseException(ex.getMessage(), 0);  
  •         }  
  •   
  •         StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");  
  •         String lastMacAddress = null;  
  •   
  •         while (tokenizer.hasMoreTokens()) {  
  •             String line = tokenizer.nextToken().trim();  
  •             boolean containsLocalHost = line.indexOf(localHost) >= 0;  
  •   
  •             // see if line contains IP address  
  •             if (containsLocalHost && lastMacAddress != null) {  
  •                 return lastMacAddress;  
  •             }  
  •   
  •             // see if line contains MAC address  
  •             int macAddressPosition = line.indexOf("HWaddr");  
  •             if (macAddressPosition <= 0)  
  •                 continue;  
  •   
  •             String macAddressCandidate = line.substring(macAddressPosition + 6)  
  •                     .trim();  
  •             if (linuxIsMacAddress(macAddressCandidate)) {  
  •                 lastMacAddress = macAddressCandidate;  
  •                 continue;  
  •             }  
  •         }  
  •   
  •         ParseException ex = new ParseException("cannot read MAC address for "  
  •                 + localHost + " from [" + ipConfigResponse + "]", 0);  
  •         ex.printStackTrace();  
  •         throw ex;  
  •     }  
  •   
  •     private final static boolean linuxIsMacAddress(String macAddressCandidate) {  
  •         // TODO: use a smart regular expression  
  •         if (macAddressCandidate.length() != 17)  
  •             return false;  
  •         return true;  
  •     }  
  •   
  •     private final static String linuxRunIfConfigCommand() throws IOException {  
  •         Process p = Runtime.getRuntime().exec("ifconfig");  
  •         InputStream stdoutStream = new BufferedInputStream(p.getInputStream());  
  •   
  •         StringBuffer buffer = new StringBuffer();  
  •         for (;;) {  
  •             int c = stdoutStream.read();  
  •             if (c == -1)  
  •                 break;  
  •             buffer.append((char) c);  
  •         }  
  •         String outputText = buffer.toString();  
  •   
  •         stdoutStream.close();  
  •   
  •         return outputText;  
  •     }  
  •   
  •     /* 
  •      * Windows stuff 
  •      */  
  •     private final static String windowsParseMacAddress(String ipConfigResponse)  
  •             throws ParseException {  
  •         String localHost = null;  
  •         try {  
  •             localHost = InetAddress.getLocalHost().getHostAddress();  
  •         } catch (java.net.UnknownHostException ex) {  
  •             ex.printStackTrace();  
  •             throw new ParseException(ex.getMessage(), 0);  
  •         }  
  •   
  •         StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");  
  •         String lastMacAddress = null;  
  •   
  •         while (tokenizer.hasMoreTokens()) {  
  •             String line = tokenizer.nextToken().trim();  
  •   
  •             // see if line contains IP address  
  •             if (line.endsWith(localHost) && lastMacAddress != null) {  
  •                 return lastMacAddress;  
  •             }  
  •   
  •             // see if line contains MAC address  
  •             int macAddressPosition = line.indexOf(":");  
  •             if (macAddressPosition <= 0)  
  •                 continue;  
  •   
  •             String macAddressCandidate = line.substring(macAddressPosition + 1)  
  •                     .trim();  
  •             if (windowsIsMacAddress(macAddressCandidate)) {  
  •                 lastMacAddress = macAddressCandidate;  
  •                 continue;  
  •             }  
  •         }  
  •   
  •         ParseException ex = new ParseException("cannot read MAC address from ["  
  •                 + ipConfigResponse + "]", 0);  
  •         ex.printStackTrace();  
  •         throw ex;  
  •     }  
  •   
  •     private final static boolean windowsIsMacAddress(String macAddressCandidate) {  
  •         // TODO: use a smart regular expression  
  •         if (macAddressCandidate.length() != 17)  
  •             return false;  
  •   
  •         return true;  
  •     }  
  •   
  •     private final static String windowsRunIpConfigCommand() throws IOException {  
  •         Process p = Runtime.getRuntime().exec("ipconfig /all");  
  •         InputStream stdoutStream = new BufferedInputStream(p.getInputStream());  
  •   
  •         StringBuffer buffer = new StringBuffer();  
  •         for (;;) {  
  •             int c = stdoutStream.read();  
  •             if (c == -1)  
  •                 break;  
  •             buffer.append((char) c);  
  •         }  
  •         String outputText = buffer.toString();  
  •   
  •         stdoutStream.close();  
  •   
  •         return outputText;  
  •     }  
  •   
  •     /* 
  •      * Main 
  •      */  
  •     public final static void main(String[] args) {  
  •         try {  
  •             System.out.println("Network infos");  
  •   
  •             System.out.println(" Operating System: "  
  •                     + System.getProperty("os.name"));  
  •             System.out.println(" IP/Localhost: "  
  •                     + InetAddress.getLocalHost().getHostAddress());  
  •             System.out.println(" MAC Address: " + getMacAddress());  
  •         } catch (Throwable t) {  
  •             t.printStackTrace();  
  •         }  
  •     }  
  • }  
    • 本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多