【发布时间】:2016-01-20 18:54:28
【问题描述】:
我正在使用 JSCH 和 Shell 对主机运行多个命令。一切正常,但我的问题是如何获取 System.out 并将其保存到文件中。我正在寻找复制而不是重定向。我可以做一个或另一个,但不能同时做。
try (OutputStream logOutput = new BufferedOutputStream(new FileOutputStream(outputFilePath))) {
try (InputStream login = new BufferedInputStream(new FileInputStream(outputFilePath))) {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(getProperties());
session.connect(10 * 1000);
Channel channel = session.openChannel("shell");
//channel.setOutputStream(System.out);// I want to activate it as well as the following command
channel.setOutputStream(logOutPut, true);// I am writing it to file
try (PipedInputStream commandSource = new PipedInputStream();
OutputStream commandSink = new PipedOutputStream(commandSource)) {
CommandSender sender = new CommandSender(commandSink);
Thread sendThread = new Thread(sender);
sendThread.start();
channel.setInputStream(commandSource);
channel.connect(15 * 1000);
sendThread.join();
if (sender.exception != null) {
throw sender.exception;
}
}
channel.disconnect();
session.disconnect();
【问题讨论】:
标签: java shell outputstream jsch