java 获取网卡物理地址
2./* 3. * Created on 2005-6-5 4. * Author stephen 5. * Email zhoujianqiang AT gmail DOT com 6. * CopyRight(C)2005-2008 , All rights reserved. 7. */ 8.import java.io.BufferedReader; 9.import java.io.IOException; 10.import java.io.InputStreamReader; 11. 12./** 13. * 与系统相关的一些常用工具方法. 14. * 15. * @author stephen 16. * @version 1.0.0 17. */ 18.public class SystemTool { 19. 20. /** 21. * 获取当前操作系统名称. 22. * return 操作系统名称 例如:windows xp,linux 等. 23. */ 24. public static String getOSName() { 25. return System.getProperty("os.name").toLowerCase(); 26. } 27. 28. /** 29. * 获取unix网卡的mac地址. 30. * 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法. 31. * @return mac地址 32. */ 33. public static String getUnixMACAddress() { 34. String mac = null; 35. BufferedReader bufferedReader = null; 36. Process process = null; 37. try { 38. process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息 39. bufferedReader = new BufferedReader(new InputStreamReader(process 40. .getInputStream())); 41. String line = null; 42. int index = -1; 43. while ((line = bufferedReader.readLine()) != null) { 44. index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr] 45. if (index >= 0) {// 找到了 46. mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址并去除2边空格 47. break; 48. } 49. } 50. } catch (IOException e) { 51. e.printStackTrace(); 52. } finally { 53. try { 54. if (bufferedReader != null) { 55. bufferedReader.close(); 56. } 57. } catch (IOException e1) { 58. e1.printStackTrace(); 59. } 60. bufferedReader = null; 61. process = null; 62. } 63. 64. return mac; 65. } 66. 67. /** 68. * 获取widnows网卡的mac地址. 69. * @return mac地址 70. */ 71. public static String getWindowsMACAddress() { 72. String mac = null; 73. BufferedReader bufferedReader = null; 74. Process process = null; 75. try { 76. process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息 77. bufferedReader = new BufferedReader(new InputStreamReader(process 78. .getInputStream())); 79. String line = null; 80. int index = -1; 81. while ((line = bufferedReader.readLine()) != null) { 82. index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address] 83. if (index >= 0) {// 找到了 84. index = line.indexOf(":");// 寻找":"的位置 85. if (index>=0) { 86. mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格 87. } 88. break; 89. } 90. } 91. } catch (IOException e) { 92. e.printStackTrace(); 93. } finally { 94. try { 95. if (bufferedReader != null) { 96. bufferedReader.close(); 97. } 98. } catch (IOException e1) { 99. e1.printStackTrace(); 100. } 101. bufferedReader = null; 102. process = null; 103. } 104. 105. return mac; 106. } 107. 108. /** 109. * 测试用的main方法. 110. * 111. * @param argc 112. * 运行参数. 113. */ 114. public static void main(String[] argc) { 115. String os = getOSName(); 116. System.out.println(os); 117. if(os.startsWith("windows")){ 118. //本地是windows 119. String mac = getWindowsMACAddress(); 120. System.out.println(mac); 121. }else{ 122. //本地是非windows系统 一般就是unix 123. String mac = getUnixMACAddress(); 124. System.out.println(mac); 125. } 126. } 127.}
相关推荐
Micusd 2020-11-19
cheidou 2020-11-19
星马殇 2020-11-18
Echodat 2020-10-08
flyToSkyL 2020-09-11
zjc 2020-09-03
阳光普照 2020-09-01
十年砍柴 2020-08-30
猪猪侠喜欢躲猫猫 2020-08-17
ladewang 2020-08-17
渣渣灰 2020-08-13
blankt 2020-08-15
vickay 2020-08-09
TinyDolphin 2020-08-09
山兔与孟婆 2020-08-09
longjing 2020-08-07
xiaogoua 2020-08-06