PageRenderTime 75ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/zzsh/WEB/src/com/common/utils/MacTool.java

https://gitlab.com/BGCX262/zzsh-svn-to-git
Java | 399 lines | 321 code | 40 blank | 38 comment | 48 complexity | 5eed431962fb2e744ac1894124cee4ac MD5 | raw file
  1. /**
  2. *
  3. */
  4. package com.common.utils;
  5. import java.io.BufferedInputStream;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.net.InetAddress;
  10. import java.text.ParseException;
  11. import java.util.StringTokenizer;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import org.apache.commons.lang.StringUtils;
  15. /**
  16. * @author Administrator
  17. *
  18. */
  19. public class MacTool {
  20. private boolean bVista=false;
  21. public String getMacAddress()
  22. {
  23. String mac = "";
  24. String os = System.getProperty("os.name");
  25. //System.out.println("--->"+os);
  26. try
  27. {
  28. if(os.startsWith("Windows"))
  29. {
  30. if(os.startsWith("Windows Vista")||os.startsWith("Windows 2008"))
  31. bVista=true;
  32. mac = windowsParseMacAddress(windowsRunIpConfigCommand());
  33. }
  34. else if(os.startsWith("Linux"))
  35. {
  36. mac = linuxParseMacAddress(linuxRunIfConfigCommand());
  37. }
  38. else if(os.startsWith("Mac OS X"))
  39. {
  40. mac = osxParseMacAddress(osxRunIfConfigCommand());
  41. }
  42. else if(os.startsWith("AIX"))
  43. {
  44. mac=aixParseNetstatMax(aiXRunIfConfigCommand());
  45. }
  46. //HP-UX
  47. else if(os.startsWith("HP-UX"))
  48. {
  49. mac=hpUxParseNetstatMax(hpUxRunIfConfigCommand());
  50. }
  51. }
  52. catch(Exception ex)
  53. {
  54. ex.printStackTrace();
  55. }
  56. return mac.replaceAll("-",":");
  57. }
  58. public static void main(String[] args) {
  59. try
  60. {
  61. FileInputStream in=new FileInputStream("E:\\ipconfig.txt");
  62. InputStream stdoutStream = new BufferedInputStream(in);
  63. StringBuffer buffer= new StringBuffer();
  64. for (;;)
  65. {
  66. int c = stdoutStream.read();
  67. if (c == -1) break;
  68. buffer.append((char)c);
  69. }
  70. String outputText = buffer.toString();
  71. outputText=new String(outputText.getBytes("ISO-8859-1"),"GBK");
  72. stdoutStream.close();
  73. MacTool mac=new MacTool();
  74. String macstr = mac.linuxParseMacAddress(outputText); //mac.windowsParseMacAddress(outputText);// mac.aixParseNetstatMax(outputText);
  75. //System.out.println("--->"+macstr);
  76. }
  77. catch(Exception ex)
  78. {
  79. ex.printStackTrace();
  80. }
  81. }
  82. /*
  83. * Linux stuff
  84. */
  85. private String linuxParseMacAddress(String ipConfigResponse) throws ParseException
  86. {
  87. String localHost = null;
  88. try
  89. {
  90. localHost = InetAddress.getLocalHost().getHostAddress();
  91. }
  92. catch(java.net.UnknownHostException ex)
  93. {
  94. ex.printStackTrace();
  95. throw new ParseException(ex.getMessage(), 0);
  96. }
  97. StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
  98. String lastMacAddress = null;
  99. while(tokenizer.hasMoreTokens())
  100. {
  101. String line = tokenizer.nextToken().trim();
  102. boolean containsLocalHost = line.indexOf(localHost) >= 0;
  103. // see if line contains IP address
  104. if(containsLocalHost && lastMacAddress != null)
  105. {
  106. return lastMacAddress;
  107. }
  108. // see if line contains MAC address
  109. int macAddressPosition = line.indexOf("HWaddr");
  110. if(macAddressPosition <= 0) continue;
  111. String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
  112. if(linuxIsMacAddress(macAddressCandidate))
  113. {
  114. lastMacAddress = macAddressCandidate;
  115. continue;
  116. }
  117. }
  118. ParseException ex = new ParseException("cannot read MAC address for " + localHost + " from [" + ipConfigResponse + "]", 0);
  119. ex.printStackTrace();
  120. throw ex;
  121. }
  122. private boolean linuxIsMacAddress(String macAddressCandidate)
  123. {
  124. Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
  125. Matcher m = macPattern.matcher(macAddressCandidate);
  126. return m.matches();
  127. }
  128. private String linuxRunIfConfigCommand() throws IOException
  129. {
  130. Process p = Runtime.getRuntime().exec("ifconfig");
  131. InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  132. StringBuffer buffer= new StringBuffer();
  133. for (;;)
  134. {
  135. int c = stdoutStream.read();
  136. if (c == -1) break;
  137. buffer.append((char)c);
  138. }
  139. String outputText = buffer.toString();
  140. stdoutStream.close();
  141. return outputText;
  142. }
  143. /*
  144. * Windows stuff
  145. */
  146. private String windowsParseMacAddress(String ipConfigResponse) throws ParseException
  147. {
  148. String localHost = null;
  149. try
  150. {
  151. localHost = InetAddress.getLocalHost().getHostAddress();
  152. //if(bVista)
  153. // localHost=localHost+"(��ѡ)";
  154. }
  155. catch(java.net.UnknownHostException ex)
  156. {
  157. ex.printStackTrace();
  158. throw new ParseException(ex.getMessage(), 0);
  159. }
  160. StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
  161. String lastMacAddress = null;
  162. while(tokenizer.hasMoreTokens())
  163. {
  164. String line = tokenizer.nextToken().trim();
  165. // see if line contains IP address
  166. if((line.endsWith(localHost)||line.endsWith(localHost+"(��ѡ)")) && lastMacAddress != null)
  167. {
  168. return lastMacAddress;
  169. }
  170. // see if line contains MAC address
  171. int macAddressPosition = line.indexOf(":");
  172. if(macAddressPosition <= 0) continue;
  173. String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
  174. if(windowsIsMacAddress(macAddressCandidate))
  175. {
  176. lastMacAddress = macAddressCandidate;
  177. continue;
  178. }
  179. }
  180. ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigResponse + "]", 0);
  181. ex.printStackTrace();
  182. throw ex;
  183. }
  184. private boolean windowsIsMacAddress(String macAddressCandidate)
  185. {
  186. Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
  187. Matcher m = macPattern.matcher(macAddressCandidate);
  188. return m.matches();
  189. }
  190. private String windowsRunIpConfigCommand() throws IOException
  191. {
  192. Process p = Runtime.getRuntime().exec("ipconfig /all");
  193. InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  194. StringBuffer buffer= new StringBuffer();
  195. for (;;)
  196. {
  197. int c = stdoutStream.read();
  198. if (c == -1) break;
  199. buffer.append((char)c);
  200. }
  201. String outputText = buffer.toString();
  202. outputText=new String(outputText.getBytes("ISO-8859-1"),"GBK");
  203. stdoutStream.close();
  204. return outputText;
  205. }
  206. /*
  207. * Mac OS X Stuff
  208. */
  209. private String osxParseMacAddress(String ipConfigResponse) throws ParseException
  210. {
  211. String localHost = null;
  212. try
  213. {
  214. localHost = InetAddress.getLocalHost().getHostAddress();
  215. }
  216. catch(java.net.UnknownHostException ex)
  217. {
  218. ex.printStackTrace();
  219. throw new ParseException(ex.getMessage(), 0);
  220. }
  221. StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
  222. while(tokenizer.hasMoreTokens())
  223. {
  224. String line = tokenizer.nextToken().trim();
  225. // see if line contains MAC address
  226. int macAddressPosition = line.indexOf("ether");
  227. if(macAddressPosition != 0) continue;
  228. String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
  229. if(osxIsMacAddress(macAddressCandidate))
  230. {
  231. return macAddressCandidate;
  232. }
  233. }
  234. ParseException ex = new ParseException("cannot read MAC address for " + localHost + " from [" + ipConfigResponse + "]", 0);
  235. ex.printStackTrace();
  236. throw ex;
  237. }
  238. private boolean osxIsMacAddress(String macAddressCandidate)
  239. {
  240. Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
  241. Matcher m = macPattern.matcher(macAddressCandidate);
  242. return m.matches();
  243. }
  244. private String osxRunIfConfigCommand() throws IOException
  245. {
  246. Process p = Runtime.getRuntime().exec("ifconfig");
  247. InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  248. StringBuffer buffer= new StringBuffer();
  249. for (;;)
  250. {
  251. int c = stdoutStream.read();
  252. if (c == -1) break;
  253. buffer.append((char)c);
  254. }
  255. String outputText = buffer.toString();
  256. stdoutStream.close();
  257. return outputText;
  258. }
  259. /**
  260. * �����ַ�ȡ�ö�Ӧ�ĵ�ַ��
  261. * @param content
  262. * @return
  263. */
  264. private String aixParseNetstatMax(String netConfigResponse)throws ParseException
  265. {
  266. StringTokenizer tokenizer = new StringTokenizer(netConfigResponse, "\n");
  267. while(tokenizer.hasMoreTokens())
  268. {
  269. String line = tokenizer.nextToken().trim();
  270. // see if line contains MAC address
  271. int macAddressPosition = line.indexOf("en");
  272. if(macAddressPosition != 0)
  273. continue;
  274. String[] conarr=StringUtils.split(line," ");
  275. String macaddr=conarr[3];
  276. String linkstr=conarr[2];
  277. if(linkstr.startsWith("link#"))
  278. {
  279. String[] arr=StringUtils.split(macaddr, ".");
  280. StringBuffer buf=new StringBuffer();
  281. for(int i=0;i<arr.length;i++)
  282. {
  283. String tmp=arr[i];
  284. if(tmp.length()==1)
  285. {
  286. tmp="0"+arr[i];
  287. }
  288. buf.append(tmp);
  289. buf.append("-");
  290. }
  291. buf.setLength(buf.length()-1);
  292. macaddr=buf.toString();
  293. return macaddr;
  294. }
  295. }
  296. ParseException ex = new ParseException("cannot read MAC address from [" + netConfigResponse + "]", 0);
  297. ex.printStackTrace();
  298. throw ex;
  299. }
  300. /**
  301. * aix
  302. * @return
  303. * @throws IOException
  304. */
  305. private String aiXRunIfConfigCommand() throws IOException
  306. {
  307. Process p = Runtime.getRuntime().exec("netstat -in");
  308. InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  309. StringBuffer buffer= new StringBuffer();
  310. for (;;)
  311. {
  312. int c = stdoutStream.read();
  313. if (c == -1) break;
  314. buffer.append((char)c);
  315. }
  316. String outputText = buffer.toString();
  317. stdoutStream.close();
  318. return outputText;
  319. }
  320. private String hpUxRunIfConfigCommand() throws IOException
  321. {
  322. Process p = Runtime.getRuntime().exec("lanscan");
  323. InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
  324. StringBuffer buffer= new StringBuffer();
  325. for (;;)
  326. {
  327. int c = stdoutStream.read();
  328. if (c == -1) break;
  329. buffer.append((char)c);
  330. }
  331. String outputText = buffer.toString();
  332. stdoutStream.close();
  333. return outputText;
  334. }
  335. private String hpUxParseNetstatMax(String netConfigResponse)throws ParseException
  336. {
  337. StringTokenizer tokenizer = new StringTokenizer(netConfigResponse, "\n");
  338. while(tokenizer.hasMoreTokens())
  339. {
  340. String line = tokenizer.nextToken().trim();
  341. // see if line contains MAC address
  342. int macAddressPosition = line.indexOf("lan0");
  343. if(macAddressPosition ==-1)
  344. continue;
  345. String[] conarr=StringUtils.split(line," ");
  346. String macaddr=conarr[1];
  347. if(macaddr.length()>2)
  348. {
  349. macaddr=macaddr.substring(2);
  350. }
  351. macaddr=macaddr.substring(0, 2)+"-"+macaddr.substring(2, 4)+"-"+macaddr.substring(4, 6)+"-"+macaddr.substring(6, 8)+"-"+
  352. macaddr.substring(8,10)+"-"+macaddr.substring(10);
  353. return macaddr;
  354. }
  355. ParseException ex = new ParseException("cannot read MAC address from [" + netConfigResponse + "]", 0);
  356. ex.printStackTrace();
  357. throw ex;
  358. }
  359. }