如何查找远程系统MAC地址

时间:2013-11-21 09:38:50

标签: java

我可以使用以下代码获取本地MAC地址

package com.eiw.server;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

class FindMACAddress {
    public static void main(String[] args) {
        InetAddress ip;
        try {
            ip = InetAddress.getLocalHost();

            System.out.println("The mac Address of this machine is :"
                    + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            System.out.print("The mac address is : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i],
                        (i < mac.length - 1) ? "-" : ""));
            }

            System.out.println(sb.toString());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

但是我需要找到远程系统Mac地址...... 是否有可能?我已经阅读了一些帖子......但不清楚......

6 个答案:

答案 0 :(得分:1)

你可以获得远程主机调用函数 getMacAddrHost(“192.168.1.xx”)的mac addr。它可能不是最好的解决方案,但效果很好。请注意,这仅适用于局域网内部。

public static String getMacAddrHost(String host) throws IOException, InterruptedException {
        //
        boolean ok = ping3(host);
        //
        if (ok) {
            InetAddress address = InetAddress.getByName(host);
            String ip = address.getHostAddress();
            return run_program_with_catching_output("arp -a " + ip);
        }
        //
        return null;
        //
    }


 public static boolean ping3(String host) throws IOException, InterruptedException {
        boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");

        ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows ? "-n" : "-c", "1", host);
        Process proc = processBuilder.start();

        int returnVal = proc.waitFor();
        return returnVal == 0;
    }

    public static String run_program_with_catching_output(String param) throws IOException {
        Process p = Runtime.getRuntime().exec(param);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            if (!line.trim().equals("")) {
                // keep only the process name
                line = line.substring(1);
                String mac = extractMacAddr(line);
                if (mac.isEmpty() == false) {
                    return mac;
                }
            }

        }
        return null;
    }

    public static String extractMacAddr(String str) {
        String arr[] = str.split("   ");
        for (String string : arr) {
            if (string.trim().length() == 17) {
                return string.trim().toUpperCase();
            }
        }
        return "";
    }

答案 1 :(得分:0)

这取决于。如果可以连接到远程系统,则可以执行ifconfig / ipconfig命令,并从输出仪表执行该机器的mac地址。但如果您无法在远程计算机上连接和执行命令,我认为没有办法知道该计算机的MAC地址。

答案 2 :(得分:0)

当两个系统位于同一网段(同一局域网,中间没有IP路由器)时,您可以通过标准网络方式获取其他系统的MAC地址

Query ARP cache to get MAC ID似乎回答了你的问题

答案 3 :(得分:0)

arp -a会显示有效的关联。例如:

  

接口:10.0.0.9 --- 0x19
   |互联网地址|物理地址|类型|

     

| 10.0.0.1 | c4-3d-c7-68-82-87 |动态|

我在这台机器上有awk,所以以下内容将为我输出MAC地址。我也在寻找一种在代码中实现这一点的方法(以与系统无关的方式)。

这可能会解决你所寻找的问题(在Java中用Process p = Runtime.getRuntime().exec("Enter command here")之类的东西包装它):

arp -a | awk "/10.0.0.1/"'  { gsub(/-/, "", $2); print toupper($2)}

输出:

  

C43DC7688287

答案 4 :(得分:0)

private static String getMacAdressByUseArp(String ip) throws IOException {
    String cmd = "arp -a " + ip;
    Scanner s = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream());
    String str = null;
    Pattern pattern = Pattern.compile("(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}\\.){2}[0-9A-Fa-f]{4})");
    try {
        while (s.hasNext()) {
            str = s.next();
            Matcher matcher = pattern.matcher(str);
            if (matcher.matches()){
                break;
            }
            else{
                str = null;
            }
        }
    }
    finally {
        s.close();
    }
    return (str != null) ? str.toUpperCase(): null;
}

答案 5 :(得分:-1)

您可以使用HttpServletRequest

获取客户端IP地址和MAC地址

参考链接:Link

public void clientIpAndMacAddress(HttpServletRequest request)
{
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String userIpAddress = httpServletRequest.getHeader("X-Forwarded-For");
    if (userIpAddress == null) {
        userIpAddress = request.getRemoteAddr();
    }
    System.out.println("Ip address : " + userIpAddress);

    String str = "";
    String macAddress = "";
    try {
        Process p = Runtime.getRuntime()
                .exec("nbtstat -A " + userIpAddress);
        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);
    }
    System.out.println("Mac address : " + macAddress);
}
相关问题