【发布时间】:2018-09-18 18:33:54
【问题描述】:
我想在Linux的shell中依次执行telnet和msh。我可以使用以下 Java 代码执行 telnet 命令,但不能执行 msh 命令:
List<String> commands = new ArrayList<String>();
commands.add("/bin/bash");
commands.add("-c");
commands.add("telnet 10.x.x.x 1234");
commands.add("msh");
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File("/home/user"));
pb.redirectErrorStream(true);
Process process = pb.start();
// Read output
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null, previous = null;
while ((line = br.readLine()) != null) {
if (!line.equals(previous)) {
previous = line;
out.append(line).append('\n');
System.out.println(line);
}
}
// Check result
if (process.waitFor() == 0) {
System.out.println("Success executing telnet command!");
System.exit(0);
}
System.err.println(commands);
System.err.println(out.toString());
System.exit(1);
非常感谢您对此的任何帮助。
【问题讨论】:
-
如何连接所有用 & 分隔的命令?
-
@BarathVutukuri,连接所有命令将不会在提示更改(#)到(msh
标签: java shell processbuilder