在Java中,可以通过执行系统命令来获取IP对应的MAC地址。下面是一个示例代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ArpMacAddress {
    public static void main(String[] args) {
        try {
            String ip = "192.168.0.1"; // 要查询的IP地址
            
            // 执行arp命令
            Process process = Runtime.getRuntime().exec("arp -a " + ip);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            
            // 读取命令输出
            String line;
            while ((line = reader.readLine()) != null) {
                // 找到IP对应的MAC地址
                if (line.contains(ip)) {
                    int index = line.indexOf("at") + 3;
                    String mac = line.substring(index, index + 17);
                    System.out.println("IP: " + ip + ", MAC: " + mac);
                    break;
                }
            }
            
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上代码通过执行arp -a命令来获取ARP缓存表中指定IP对应的MAC地址。注意替换ip变量为你要查询的IP地址

java 根据arp命令获取IP对应的mac地址

原文地址: https://www.cveoy.top/t/topic/iehD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录