Log stdout and stderr from ssh command in the same order it was created

99封情书 提交于 2019-11-29 08:48:56
ymnk

How about the following chunk of code?

channel.setCommand(command);

PipedOutputStream pos=new PipedOutputStream();
PipedInputStream pis=new PipedInputStream(pos);
channel.setOutputStream(pos);
channel.setExtOutputStream(pos);
InputStream in=pis;

channel.connect();

You are working with separate streams, so no, there is no way you can possibly guarantee to get the exact same output.

that said, your code has a number of potential problems, among them:

  • InputStream.available is a generally useless method and should be avoided
  • you are reading bytes and converting arbitrary byte arrays to chars/Strings, which could result in broken output (if you are getting multi-byte chars you could end up splitting them).
  • reading 2 different blocking streams with a single thread can result in deadlock
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!