如何使用 Java 5 获取主机 MAC 地址?

发布于 2024-08-03 03:59:59 字数 156 浏览 3 评论 0原文

我知道您可以使用 Java 6 使用 java.net.NetworkInterface->getHardwareAddress() 来完成此操作。但我部署的环境仅限于 Java 5。

有谁知道如何在 Java 5 或更早版本中执行此操作?非常感谢。

I know you can do this with Java 6 using java.net.NetworkInterface->getHardwareAddress(). But the environment I am deploying on is restricted to Java 5.

Does anyone know how to do this in Java 5 or earlier? Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朱染 2024-08-10 03:59:59

Java 5 中的标准方法是启动本机进程来运行 ipconfigifconfig 并解析 OutputStream 以获得答案。

例如:

private String getMacAddress() throws IOException {
    String command = “ipconfig /all”;
    Process pid = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
    Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”);
    while (true) {
        String line = in.readLine();
        if (line == null)
            break;
        Matcher m = p.matcher(line);
        if (m.matches()) {
            return m.group(1);
        }
    }
}

The standard way in Java 5 was to start a native process to run ipconfig or ifconfig and parse the OutputStream to get your answer.

For example:

private String getMacAddress() throws IOException {
    String command = “ipconfig /all”;
    Process pid = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
    Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”);
    while (true) {
        String line = in.readLine();
        if (line == null)
            break;
        Matcher m = p.matcher(line);
        if (m.matches()) {
            return m.group(1);
        }
    }
}
行至春深 2024-08-10 03:59:59

Butterchicken 的解决方案没问题,但仅适用于英文版本的 Windows。

一个更好的(独立于语言的)解决方案是匹配 MAC 地址的模式。在这里我还确保该地址具有关联的 IP(例如,过滤掉蓝牙设备):

public String obtainMacAddress()
throws Exception {
    Process aProc = Runtime.getRuntime().exec("ipconfig /all");
    InputStream procOut = new DataInputStream(aProc.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(procOut));

    String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})";
    Pattern aPatternMac = Pattern.compile(aMacAddress);
    String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$";
    Pattern aPatternIp = Pattern.compile(aIpAddress);
    String aNewAdaptor = "[A-Z].*$";
    Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor);

    // locate first MAC address that has IP address
    boolean zFoundMac = false;
    boolean zFoundIp = false;
    String foundMac = null;
    String theGoodMac = null;

    String strLine;
    while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) {
        Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine);
        if (aMatcherNewAdaptor.matches()) {
            zFoundMac = zFoundIp = false;
        }
        Matcher aMatcherMac = aPatternMac.matcher(strLine);
        if (aMatcherMac.find()) {
            foundMac = aMatcherMac.group(0);
            zFoundMac = true;
        }
        Matcher aMatcherIp = aPatternIp.matcher(strLine);
        if (aMatcherIp.matches()) {
            zFoundIp = true;
            if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac;
        }
    }

    aProc.destroy();
    aProc.waitFor();

    return theGoodMac;
}

Butterchicken's solution is ok, but will only work on english versions of Windows.

A somewhat better (language independent) solution would be to match the pattern for MAC addresses. Here I also make sure that this address has an associated IP (e.g. to filter out bluetooth devices):

public String obtainMacAddress()
throws Exception {
    Process aProc = Runtime.getRuntime().exec("ipconfig /all");
    InputStream procOut = new DataInputStream(aProc.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(procOut));

    String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})";
    Pattern aPatternMac = Pattern.compile(aMacAddress);
    String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$";
    Pattern aPatternIp = Pattern.compile(aIpAddress);
    String aNewAdaptor = "[A-Z].*$";
    Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor);

    // locate first MAC address that has IP address
    boolean zFoundMac = false;
    boolean zFoundIp = false;
    String foundMac = null;
    String theGoodMac = null;

    String strLine;
    while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) {
        Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine);
        if (aMatcherNewAdaptor.matches()) {
            zFoundMac = zFoundIp = false;
        }
        Matcher aMatcherMac = aPatternMac.matcher(strLine);
        if (aMatcherMac.find()) {
            foundMac = aMatcherMac.group(0);
            zFoundMac = true;
        }
        Matcher aMatcherIp = aPatternIp.matcher(strLine);
        if (aMatcherIp.matches()) {
            zFoundIp = true;
            if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac;
        }
    }

    aProc.destroy();
    aProc.waitFor();

    return theGoodMac;
}
铁轨上的流浪者 2024-08-10 03:59:59

据我所知,没有纯粹的 Java 6 之前的解决方案。 UUID 解决了这个问题,但首先确定操作系统以确定它是否应该运行 ifconfig 或 ipconfig。

As far as I know there is no pure pre Java 6 solution. UUID solves this but first determine OS to find out if it should run ifconfig or ipconfig.

韬韬不绝 2024-08-10 03:59:59

在 Linux 和 Mac OS X 计算机上,您可能必须使用 ifconfig -a
ipconfig 是 Windows 命令。

On Linux and Mac OS X Machine , you might have to use ifconfig -a.
ipconfig is as windows command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文