StringBuilder 输出为空执行 linux echo 命令
StringBuilder output is empty executing linux echo command
我正在尝试在 java 中执行此命令,我需要获取输出
在 java 字符串中。该命令使用密码短语加密纯文本,return 加密纯文本。
命令为:
/bin/sh -c 回显 "textToEncrypt" | /usr/bin/openssl enc -aes-256-cbc -e -base64 -A -pass pass:passwordPhrase
截图:
我执行以下操作:
我可以 运行 linux shell 中的命令,并且我在 shell 中得到一个加密的输出字符串,例如:"U2FsdGVkX1/1UcPzhX7IGgvXdx9YrS+cizrla8UYhU8=",所以命令有效。
我可以 运行 在 java 1.7 中使用相同的命令,如下面的示例代码(cryptData 方法)。 运行没问题,但我没有得到输出(加密数据)。
我可以 运行 任何没有来自 java 的 "echo" 的命令,例如,("ls -fla | more")。 运行 又好了,我总能得到输出。
这是代码:
public void cryptData() {
String passwordPhrase="a1b2c3d4e5";
ProcessBuilder processBuilder = new ProcessBuilder();
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("echo");
commands.add("/"textToCrypt/"");
commands.add("|");
commands.add("/usr/bin/openssl");
commands.add("-aes-256-cbc");
commands.add("-e");
commands.add("-base64");
commands.add("-A");
commands.add("-pass");
commands.add("pass:"+passwordPhrase);
// Run the shell command
processBuilder.command(commands);
try { //standard stringBuilder process
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println("Output1"+ output);
System.out.println("Output2"+ output.toString());
System.exit(0);
} else {
System.out.println("Error");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
最终结果始终是:"Success!" 但此代码的输出为空或 null。我做错了什么?
sh -c
期望命令作为单个参数执行。所以在添加 /bin/sh
和 -c
之后,您应该将命令的其余部分添加为单个参数,如下所示:
commands.add("/bin/sh");
commands.add("-c");
commands.add("echo /" + textToCrypt + "/ | /usr/bin/openssl -aes-256-cbc " +
"-e -base64 -A -pass pass: " + passwordPhrase);
我正在尝试在 java 中执行此命令,我需要获取输出
在 java 字符串中。该命令使用密码短语加密纯文本,return 加密纯文本。
命令为:
/bin/sh -c 回显 "textToEncrypt" | /usr/bin/openssl enc -aes-256-cbc -e -base64 -A -pass pass:passwordPhrase
截图:
我执行以下操作:
我可以 运行 linux shell 中的命令,并且我在 shell 中得到一个加密的输出字符串,例如:"U2FsdGVkX1/1UcPzhX7IGgvXdx9YrS+cizrla8UYhU8=",所以命令有效。
我可以 运行 在 java 1.7 中使用相同的命令,如下面的示例代码(cryptData 方法)。 运行没问题,但我没有得到输出(加密数据)。
我可以 运行 任何没有来自 java 的 "echo" 的命令,例如,("ls -fla | more")。 运行 又好了,我总能得到输出。
这是代码:
public void cryptData() {
String passwordPhrase="a1b2c3d4e5";
ProcessBuilder processBuilder = new ProcessBuilder();
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("echo");
commands.add("/"textToCrypt/"");
commands.add("|");
commands.add("/usr/bin/openssl");
commands.add("-aes-256-cbc");
commands.add("-e");
commands.add("-base64");
commands.add("-A");
commands.add("-pass");
commands.add("pass:"+passwordPhrase);
// Run the shell command
processBuilder.command(commands);
try { //standard stringBuilder process
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println("Output1"+ output);
System.out.println("Output2"+ output.toString());
System.exit(0);
} else {
System.out.println("Error");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
最终结果始终是:"Success!" 但此代码的输出为空或 null。我做错了什么?
sh -c
期望命令作为单个参数执行。所以在添加 /bin/sh
和 -c
之后,您应该将命令的其余部分添加为单个参数,如下所示:
commands.add("/bin/sh");
commands.add("-c");
commands.add("echo /" + textToCrypt + "/ | /usr/bin/openssl -aes-256-cbc " +
"-e -base64 -A -pass pass: " + passwordPhrase);