我尝试使用InputStream和Buffer[],还有BufferedReader和PipedInputStream。对于所有情况,我都得到了null:
sessionB = jSch.getSession(username, "localhost", forwardedPort);
sessionB.connect();
if(sessionB.isConnected()) {
System.out.println("Connected host B!");
channel = (ChannelExec) sessionB.openChannel("exec");
br = new BufferedReader(new
InputStreamReader(channel.getInputStream()));
((ChannelExec)channel).setCommand("command");
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
if(channel.isConnected()) {
System.out.println("Channel is connected!");
}
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}和控制台输出:
连接的主机A!连接的主机B!通道已连接!
问题:我没有打印任何东西(System.out.println(line);)
有一种方法可以通过端口转发从exec通道获取流吗?谢谢你的帮忙
发布于 2018-07-31 20:29:04
get/setInputStream和get/setOutputStream只关心命令的标准输入/输出,与端口转发无关。端口转发是通过两个函数setPortForwardingL和setPortForwardingR完成的。
您的代码中应该包含类似的内容。
int assinged_port=session.setPortForwardingL(lport, rhost, rport);一如既往,关于JSCH的文档很少,但是有很多针对L forwarding和R forwarding的detailed examples
端口转发是在会话中完成的,因此我不确定您是否也需要活动连接。但如果你这样做了,你应该考虑打开一个'shell‘连接,而不是'exec’。这样你就不必运行一个无用的命令来维护连接了。
PS:一些代码丢失了,所以很难对问题中给出的例子说一些确切的东西。
发布于 2018-07-31 23:21:37
谢谢你的帮助。我用管道解决了这个问题,我分享了我所做的:
PipedInputStream en = new PipedInputStream();
pin = new PipedOutputStream((PipedInputStream) en);
BufferedReader br = new BufferedReader(new InputStreamReader((PipedInputStream)
channel.getInputStream()));
channel.connect(5*1000);
String received=null;
while((received=br.readLine())!=null) {
System.out.println(received);
}https://stackoverflow.com/questions/51566847
复制相似问题