使用 Unix 命令在 java 中打印 Mac 的序列号

发布于 2024-11-02 08:40:57 字数 861 浏览 1 评论 0 原文

我正在尝试在 java 程序中打印我的 mac [编辑:Apple 计算机] 序列号。我熟悉

ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'

在终端中完成此任务的 Unix 命令。
当我尝试时,

String command = "ioreg -l | awk '/IOPlatformSerialNumber/ { print $4; }'"
Runtime terminal = Runtime.getRuntime(); 
String input = new BufferedReader(
    new InputStreamReader(
        terminal.exec(commands).getInputStream())).readLine();
System.out.println(new BufferedReader(
    new InputStreamReader(
        terminal.exec(command, args).getInputStream())).readLine());

我的序列号未打印。相反,它打印:

<+-o Root class IORegistryEntry, id 0x100000100, retain 10>  

我认为问题是 terminal.exec() 并不意味着获取整个命令字符串。 java中是否有类似于python的Popen(command, stdout=PIPE, shell=True) 中的参数shell = True的东西,它允许我传递整个命令字符串?

I am trying to print my mac's [edit: Apple computer] serial number in a java program. I am familiar with the Unix command

ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'

which accomplishes this task in terminal.
When I try

String command = "ioreg -l | awk '/IOPlatformSerialNumber/ { print $4; }'"
Runtime terminal = Runtime.getRuntime(); 
String input = new BufferedReader(
    new InputStreamReader(
        terminal.exec(commands).getInputStream())).readLine();
System.out.println(new BufferedReader(
    new InputStreamReader(
        terminal.exec(command, args).getInputStream())).readLine());

my serial number is not printed. Instead it prints:

<+-o Root class IORegistryEntry, id 0x100000100, retain 10>  

I think the problem is that terminal.exec() is not meant to take the whole command string. Is there something in java similar to the argument shell = True in python's Popen(command, stdout=PIPE, shell=True) that will allow me to pass the whole command string?

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

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

发布评论

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

评论(3

烟雨凡馨 2024-11-09 08:40:57

我看到两种可能性:

  1. 使用 ioreg -l 的输出/util/Scanner.html" rel="nofollow noreferrer">扫描仪

  2. 将命令包装在 shell 脚本中并 exec() 它:

#!/bin/sh
ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'

附录:As使用 ProcessBuilder 的示例,并纳入有用的建议Paul Cager,这是第三种选择:

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

public class PBTest {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("bash", "-c",
            "ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'");
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            String s;
            // read from the process's combined stdout & stderr
            BufferedReader stdout = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = stdout.readLine()) != null) {
                System.out.println(s);
            }
            System.out.println("Exit value: " + p.waitFor());
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

I see two possibilities:

  1. Parse the output of ioreg -l using, say, Scanner.

  2. Wrap the command in a shell script and exec() it:

#!/bin/sh
ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'

Addendum: As an example of using ProcessBuilder, and incorporating a helpful suggestion by Paul Cager, here's a third alternative:

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

public class PBTest {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("bash", "-c",
            "ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'");
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            String s;
            // read from the process's combined stdout & stderr
            BufferedReader stdout = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = stdout.readLine()) != null) {
                System.out.println(s);
            }
            System.out.println("Exit value: " + p.waitFor());
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
原谅过去的我 2024-11-09 08:40:57

Runtime.exec(..) 不支持管道,因为它们是 shell 的一项功能。相反,您必须自己模拟管道,例如,

String ioreg = toString(Runtime.exec("ioreg -l ").getInputStream());

Process awk = Runtime.exec("awk '/IOPlatformSerialNumber/ { print $4;}'");
write(awk.getOutputStream(), ioreg);

String input = new BufferedReader(new InputStreamReader(awk.getInputStream())).readLine();

或者,您当然可以将 shell 作为进程运行,例如 Runtime.exec("bash"),并通过读取和写入其 IO 流来与其交互。不过,与进程交互有点棘手,并且有一些陷阱并让它执行您的命令(请参阅评论)

Pipes aren't supported by Runtime.exec(..) since they are a feature of shells. Instead, you'd have to emulate the pipe yourself, e.g.

String ioreg = toString(Runtime.exec("ioreg -l ").getInputStream());

Process awk = Runtime.exec("awk '/IOPlatformSerialNumber/ { print $4;}'");
write(awk.getOutputStream(), ioreg);

String input = new BufferedReader(new InputStreamReader(awk.getInputStream())).readLine();

Alternatively, you could of course run a shell as a process, e.g. Runtime.exec("bash"), and interact with it by reading and writing its IO streams. Interacting with processes is a bit tricky though and has some gotchas and let it execute your command (see comments)

我很OK 2024-11-09 08:40:57

要通过 Java 获取 MAC 地址,您可以使用 java.net.NetworkInterface

NetworkInterface.getByName("xxx").getHardwareAddress()

如果您不知道网络接口的名称(我假设它在 Linux 上是“eth0”),您可以甚至使用 NetworkInterface.getNetworkInterfaces() 迭代所有网络接口。

To get the MAC addres via Java you can use java.net.NetworkInterface:

NetworkInterface.getByName("xxx").getHardwareAddress()

If you don't know the name (I assume it to be 'eth0' on linux) of your network interface, you can even iterate throug all of your network interfaces using NetworkInterface.getNetworkInterfaces().

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