java通过ip地址获取远程设备mac地址,非本地的mac地址

迷南。 2023-09-30 23:38 65阅读 0赞

远程ip是 192.168.1.28

  1. public class GetMacByIp {
  2. public static void main(String[] args) throws UnknownHostException, SocketException {
  3. String ip = "192.168.1.28";
  4. String localMac = getLocalMac(ip);
  5. System.out.println(localMac);
  6. }
  7. public static String getLocalMac(String ipAddress){
  8. String str = "";
  9. String macAddress = "";
  10. // 获取非本地IP的MAC地址
  11. try {
  12. System.out.println(ipAddress);
  13. Process p = Runtime.getRuntime()
  14. .exec("nbtstat -A " + ipAddress);
  15. System.out.println("===process==" + p);
  16. InputStreamReader ir = new InputStreamReader(p.getInputStream());
  17. BufferedReader br = new BufferedReader(ir);
  18. while ((str = br.readLine()) != null) {
  19. if (str.indexOf("MAC") > 1) {
  20. macAddress = str.substring(str.indexOf("MAC") + 9, str.length());
  21. macAddress = macAddress.trim();
  22. System.out.println("macAddress:" + macAddress);
  23. break;
  24. }
  25. }
  26. p.destroy();
  27. br.close();
  28. ir.close();
  29. } catch (IOException ex) {
  30. }
  31. return macAddress;
  32. }
  33. }

测试结果:
在这里插入图片描述

发表评论

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

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

相关阅读