Jsch OutputStream未使用Java显示完整的服务器输出
我使用JSCH在服务器上运行多个命令。一切正常,但如果命令的输出很长,捕获服务器响应时会出现问题。如果命令的输出很长,outputStream只显示通过putty会话显示的normaly,为了查看完整的输出,您必须按enter键在两行之间移动,或按q键退出Jsch OutputStream未使用Java显示完整的服务器输出,java,outputstream,jsch,Java,Outputstream,Jsch,我使用JSCH在服务器上运行多个命令。一切正常,但如果命令的输出很长,捕获服务器响应时会出现问题。如果命令的输出很长,outputStream只显示通过putty会话显示的normaly,为了查看完整的输出,您必须按enter键在两行之间移动,或按q键退出 A Sample of a long server response that you need to hit enter to continue. 代码: 您正在执行more命令。解决方案:不要。更新图像后,我得到了一些东西的第1行,
A Sample of a long server response that you need to hit enter to continue.
代码:
您正在执行
more
命令。解决方案:不要。更新图像后,我得到了一些东西的第1行,我必须继续按enter键。
public static void main(String args[]) throws JSchException, InterruptedException, IOException {
extractResources();
TMPFS jssh = new TMPFS();
// Local class for feeding commands to a pipe in a background thread.
class CommandSender
implements Runnable {
private final OutputStream target;
IOException exception;
CommandSender(OutputStream target) {
this.target = Objects.requireNonNull(target);
}
@Override
public void run() {
try {
for (String command : commands) {
target.write(command.getBytes());
target.write(10); // newline
TimeUnit.SECONDS.sleep(3);
}
} catch (IOException e) {
exception = e;
} catch (InterruptedException e) {
System.out.println("Interrupted, exiting prematurely.");
}
}
}
try (OutputStream logout = new BufferedOutputStream(new FileOutputStream(outputFilePath))) {
try (InputStream login = new BufferedInputStream(new FileInputStream(outputFilePath))) {
JSch jsch = new JSch();
if (!hosts.isEmpty() && user != null && password != null) {
for (String host : hosts) {
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(getProperties());
session.connect(10 * 1000);
if (session.isConnected()) {
System.out.println(session.getHost() + " Connected");
}
Channel channel = session.openChannel("shell");
channel.setInputStream(login, true);
channel.setOutputStream(logout, true);
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);
System.out.println("Commands executed: \n" + session.getHost() + " disconnected");
sendThread.join();
if (sender.exception != null) {
throw sender.exception;
}
}
channel.disconnect();
session.disconnect();
if (!session.isConnected()) {
System.out.println("Commands executed \n" + session.getHost() + " disconnected");
}
}
}