gpt4 book ai didi

java - 使用 apache commons exec 运行管道命令的更好方法

转载 作者:行者123 更新时间:2023-11-30 10:16:37 25 4
gpt4 key购买 nike

我正在使用 apache commons exec 来运行命令:arp | wc -l下面是我的代码:

 private String runCommand(String cmd, String params) {
CommandLine commandLine = new CommandLine(cmd);
commandLine.addArguments(params);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); // 30s timeout
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);

try {
int retCode = executor.execute(commandLine);
System.out.println("Executed '" + cmd + "'\n"
+ "returnCode: " + retCode + "\n"
+ "stdout:\n" + stdout.toString() + "\n"
+ "stderr:\n" + stderr.toString());

if (retCode == 0) {
return stdout.toString();
} else {
throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
}

} catch (IOException e) {
throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
}
}

这里的cmd是/bin/sh params是-c arp|wc-l代码给出以下输出:

Executed '/bin/sh'
returnCode: 0
stdout:
54 71 4321

stderr:
usage: arp [-n] [-i interface] hostname
arp [-n] [-i interface] [-l] -a
arp -d hostname [pub] [ifscope interface]
arp -d [-i interface] -a
arp -s hostname ether_addr [temp] [reject] [blackhole] [pub [only]] [ifscope interface]
arp -S hostname ether_addr [temp] [reject] [blackhole] [pub [only]] [ifscope interface]
arp -f filename

我有两个问题:
问题 1。我不明白为什么我的输出中有三个数字 (54 71 4321)。不是应该只有一个数字吗?

问题 2。有没有更好的方法来使用 apache commons exec 运行相同的命令?

最佳答案

仔细阅读文档后- https://commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/CommandLine.html .
这是问题 2 的答案:

private String runCommand(String cmd, String[] params) {
CommandLine commandLine = new CommandLine(cmd);
commandLine.addArguments(params, false);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); // 30s timeout
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);

try {
int retCode = executor.execute(commandLine);
System.out.println("Executed '" + cmd + "'\n"
+ "returnCode: " + retCode + "\n"
+ "stdout:\n" + stdout.toString() + "\n"
+ "stderr:\n" + stderr.toString());

if (retCode == 0) {
return stdout.toString();
} else {
throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
}

} catch (IOException e) {
throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
}
}

这里cmd是:/bin/sh参数是:new String[] { "-c", "arp|wc -l"}

关于java - 使用 apache commons exec 运行管道命令的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com