JAVA程序 通过IP地址 获取MAC地址

缺乏、安全感 2023-06-24 14:23 61阅读 0赞

转载自:https://blog.csdn.net/chwshuang/article/details/46958719

叙述

我们都知道通过在电脑上敲 cmd 后,输入ipconfig -all 来获取IP地址或者MAC地址,java程序可以在window和linux下获取MAC地址等信息!

代码

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.LineNumberReader;
  5. /**
  6. *
  7. * @author http://zhidao.baidu.com/question/292753600.html
  8. */
  9. public class TestMac {
  10. public static void main(String[] args) {
  11. long start = System.currentTimeMillis();
  12. System.out.println("Operation System=" + getOsName());
  13. System.out.println("Mac Address=" + getMACAddress());
  14. System.out.println("通过ip获取mac地址为:" + getMACAddress("192.168.2.14"));
  15. long end = System.currentTimeMillis();
  16. System.out.println("获取电脑MAC地址时间为:" + (end - start));
  17. }
  18. /**
  19. * 获取操作系统名称
  20. */
  21. public static String getOsName() {
  22. String os = "";
  23. os = System.getProperty("os.name");
  24. return os;
  25. }
  26. /**
  27. * 获取MAC地址
  28. */
  29. public static String getMACAddress() {
  30. String address = "";
  31. String os = getOsName();
  32. //根据操作系统类型获取MAC地址
  33. if (os.startsWith("Windows")) {
  34. try {
  35. String command = "cmd.exe /c ipconfig /all";
  36. Process p = Runtime.getRuntime().exec(command);
  37. BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
  38. String line;
  39. while ((line = br.readLine()) != null) {
  40. if (line.indexOf("Physical Address") > 0) {
  41. int index = line.indexOf(":");
  42. index += 2;
  43. address = line.substring(index);
  44. break;
  45. }
  46. }
  47. br.close();
  48. return address.trim();
  49. } catch (IOException e) {
  50. }
  51. } else if (os.startsWith("Linux")) {
  52. String command = "/bin/sh -c ifconfig -a";
  53. Process p;
  54. try {
  55. p = Runtime.getRuntime().exec(command);
  56. BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
  57. String line;
  58. while ((line = br.readLine()) != null) {
  59. if (line.indexOf("HWaddr") > 0) {
  60. int index = line.indexOf("HWaddr") + "HWaddr".length();
  61. address = line.substring(index);
  62. break;
  63. }
  64. }
  65. br.close();
  66. } catch (IOException e) {
  67. }
  68. }
  69. address = address.trim();
  70. return address;
  71. }
  72. /**
  73. * 根据IP地址获取MAC地址
  74. */
  75. public static String getMACAddress(String ipAddress) {
  76. String str = "", strMAC = "", macAddress = "";
  77. try {
  78. Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
  79. InputStreamReader ir = new InputStreamReader(pp.getInputStream());
  80. LineNumberReader input = new LineNumberReader(ir);
  81. for (int i = 1; i < 100; i++) {
  82. str = input.readLine();
  83. if (str != null) {
  84. if (str.indexOf("MAC Address") > 1) {
  85. strMAC = str.substring(str.indexOf("MAC Address") + 14, str.length());
  86. break;
  87. }
  88. }
  89. }
  90. } catch (IOException ex) {
  91. return "Can't Get MAC Address!";
  92. }
  93. if (strMAC.length() < 17) {
  94. return "Error!";
  95. }
  96. macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) + ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) + ":"
  97. + strMAC.substring(12, 14) + ":" + strMAC.substring(15, 17);
  98. return macAddress;
  99. }
  100. }

注意:

这段代码需要公网访问的服务器上部署后有效,自己的机器上属于局域网,一般获取的都是本地IP,效果不同

发表评论

表情:
评论列表 (有 0 条评论,61人围观)

还没有评论,来说两句吧...

相关阅读