java获取本机ip,mac,

  1 import java.io.BufferedReader;   
  2 import java.io.IOException;   
  3 import java.io.InputStreamReader;   
  4   
  5 /**  
  6  * 获取MAC地址  
  7  * @author sunlightcs  
  8  *  
  9  */  
 10 public class GetMacAddress {   
 11   
 12       /**   
 13        *  获取当前操作系统名称.   
 14        *  return 操作系统名称 例如:windows,Linux,Unix等.   
 15       */     
 16      public static String getOSName() {     
 17          return System.getProperty("os.name").toLowerCase();     
 18      }     
 19               
 20      /**   
 21        * 获取Unix网卡的mac地址.   
 22       * @return mac地址   
 23       */     
 24      public static String getUnixMACAddress() {     
 25          String mac = null;     
 26          BufferedReader bufferedReader = null;     
 27          Process process = null;     
 28          try {     
 29                /**  
 30                 *  Unix下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息    
 31                 */  
 32              process = Runtime.getRuntime().exec("ifconfig eth0");   
 33              bufferedReader = new BufferedReader(new InputStreamReader(process     
 34                      .getInputStream()));     
 35              String line = null;     
 36              int index = -1;     
 37              while ((line = bufferedReader.readLine()) != null) {     
 38                     /**  
 39                      *  寻找标示字符串[hwaddr]   
 40                      */  
 41                  index = line.toLowerCase().indexOf("hwaddr");    
 42                     /**  
 43                      * 找到了  
 44                      */  
 45                  if (index != -1) {     
 46                         /**  
 47                          *   取出mac地址并去除2边空格    
 48                          */  
 49                      mac = line.substring(index +"hwaddr".length()+ 1).trim();   
 50                      break;     
 51                  }     
 52              }     
 53          } catch (IOException e) {     
 54              e.printStackTrace();     
 55          } finally {     
 56              try {     
 57                  if (bufferedReader != null) {     
 58                      bufferedReader.close();     
 59                  }     
 60              } catch (IOException e1) {     
 61                 e1.printStackTrace();     
 62             }     
 63              bufferedReader = null;     
 64              process = null;     
 65          }     
 66       
 67          return mac;     
 68      }     
 69           
 70             
 71             
 72          /**   
 73            * 获取Linux网卡的mac地址.   
 74           * @return mac地址   
 75           */     
 76          public static String getLinuxMACAddress() {     
 77              String mac = null;     
 78              BufferedReader bufferedReader = null;     
 79              Process process = null;     
 80              try {     
 81                    /**  
 82                     *  linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息    
 83                     */  
 84                  process = Runtime.getRuntime().exec("ifconfig eth0");   
 85                  bufferedReader = new BufferedReader(new InputStreamReader(process     
 86                          .getInputStream()));     
 87                  String line = null;     
 88                  int index = -1;     
 89                  while ((line = bufferedReader.readLine()) != null) {     
 90                      index = line.toLowerCase().indexOf("硬件地址");    
 91                         /**  
 92                          * 找到了  
 93                          */  
 94                      if (index != -1) {     
 95                             /**  
 96                              *   取出mac地址并去除2边空格    
 97                              */  
 98                          mac = line.substring(index+4).trim();   
 99                          break;     
100                      }     
101                  }     
102              } catch (IOException e) {     
103                  e.printStackTrace();     
104              } finally {     
105                  try {     
106                      if (bufferedReader != null) {     
107                          bufferedReader.close();     
108                      }     
109                  } catch (IOException e1) {     
110                     e1.printStackTrace();     
111                 }     
112                  bufferedReader = null;     
113                  process = null;     
114              }   
115           
116              return mac;     
117          }   
118             
119          /**   
120           * 获取widnows网卡的mac地址.   
121           * @return mac地址   
122           */     
123          public static String getWindowsMACAddress() {     
124              String mac = null;     
125              BufferedReader bufferedReader = null;     
126              Process process = null;     
127              try {     
128                    /**  
129                     * windows下的命令,显示信息中包含有mac地址信息    
130                     */  
131                  process = Runtime.getRuntime().exec("ipconfig /all");   
132                  bufferedReader = new BufferedReader(new InputStreamReader(process     
133                          .getInputStream()));     
134                  String line = null;     
135                  int index = -1;     
136                  while ((line = bufferedReader.readLine()) != null) {     
137                         /**  
138                          *  寻找标示字符串[physical address]   
139                          */  
140                      index = line.toLowerCase().indexOf("physical address");    
141                      if (index != -1) {   
142                          index = line.indexOf(":");   
143                          if (index != -1) {   
144                                 /**  
145                                  *   取出mac地址并去除2边空格  
146                                  */  
147                             mac = line.substring(index + 1).trim();    
148                         }   
149                          break;     
150                      }   
151                  }   
152              } catch (IOException e) {     
153                  e.printStackTrace();     
154              }finally {     
155                  try {     
156                      if (bufferedReader != null) {     
157                          bufferedReader.close();     
158                        }     
159                  }catch (IOException e1) {     
160                      e1.printStackTrace();     
161                    }     
162                  bufferedReader = null;     
163                  process = null;     
164              }     
165           
166              return mac;     
167          }     
168           
169          /**   
170           * 测试用的main方法.   
171           *    
172           * @param argc   
173          *            运行参数.   
174           */     
175          public static void main(String[] argc) {     
176              String os = getOSName();     
177              System.out.println(os);     
178              if(os.startsWith("windows")){     
179                  String mac = getWindowsMACAddress();     
180                  System.out.println("本地是windows:"+mac);     
181              }else if(os.startsWith("linux")){     
182                    String mac = getLinuxMACAddress();     
183                  System.out.println("本地是Linux系统,MAC地址是:"+mac);   
184              }else{     
185                  String mac = getUnixMACAddress();                         
186                  System.out.println("本地是Unix系统 MAC地址是:"+mac);   
187              }     
188          }     
189 }

 

posted @ 2014-03-18 15:22  哎呦喂,我的小祖宗╰つ  阅读(236)  评论(0编辑  收藏  举报