Java获取IP对应的MAC地址

用Java获取MAC地址,首先要获取IP地址,然后调用DOS下的nbtstat -A ip命令来获取对应的MAC地址,具体代码如下:

  1. public String getMACAddress(String ip) {
  2. String str = "";
  3. String macAddress = "";
  4. try {
  5. Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
  6. InputStreamReader ir = new InputStreamReader(p.getInputStream());
  7. LineNumberReader input = new LineNumberReader(ir);
  8. for (int i = 1; i < 100; i++) {
  9. str = input.readLine();
  10. if (str != null) {
  11. if (str.indexOf("MAC Address") > 1) {
  12. macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
  13. break;
  14. }
  15. }
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace(System.out);
  19. }
  20. return macAddress;
  21. }


» 本文链接:https://blog.apires.cn/archives/1749.html
» 转载请注明来源:Java地带  » 《Java获取IP对应的MAC地址》

» 本文章为Java地带整理创作,欢迎转载!转载请注明本文地址,谢谢!
» 部分内容收集整理自网络,如有侵权请联系我删除!

» 订阅本站:https://blog.apires.cn/feed/

标签: Java, IP, MAC

添加新评论