为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

猛跑小猪 2019-12-06 10:48:56
我正在尝试在Java程序中打印Mac的[编辑:Apple计算机]序列号。我熟悉Unix命令ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'在终端中完成此任务。当我尝试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中是否有类似于shell = Truepython中的参数的东西Popen(command, stdout=PIPE, shell=True),可以让我传递整个命令字符串?
查看完整描述

3 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

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作为进程运行


查看完整回答
反对 回复 2019-12-06
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

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


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

如果您不知道网络接口的名称(我在Linux上假定为'eth0'),则甚至可以使用遍历所有网络接口NetworkInterface.getNetworkInterfaces()。


查看完整回答
反对 回复 2019-12-06
  • 3 回答
  • 0 关注
  • 396 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信