Java获取IP对应的MAC地址

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

public String getMACAddress(String ip) {
    String str = "";
    String macAddress = "";
    try {
        Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
        InputStreamReader ir = new InputStreamReader(p.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        for (int i = 1; i < 100; i++) {
            str = input.readLine();
            if (str != null) {
                if (str.indexOf("MAC Address") > 1) {
                    macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
                    break;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace(System.out);
    }
    return macAddress;
}


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

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

» 订阅本站:https://blog.java-zone.org/feed/

标签: Java, IP, MAC

添加新评论