简体   繁体   English

JSch-如何使用PortForwarding L从远程服务器捕获流?

[英]JSch - How to capture stream from remote server using PortForwarding L?

I tried to use InputStream and Buffer[], also BufferedReader, also PipedInputStream. 我尝试使用InputStream和Buffer [],也使用BufferedReader,也使用PipedInputStream。 For all cases i got 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);
                    }

And console output: 和控制台输出:

Connected host A! 连接主机A! Connected host B! 已连接主机B! Channel is connected! 频道已连接!

Problem: I got nothing printed (System.out.println(line);) 问题:我什么也没打印(System.out.println(line);)

There is a way to get that stream from exec channel over portforwarding ? 有一种方法可以通过portforwarding从exec通道获取该流? Thanks for your help 谢谢你的帮助

get/setInputStream and get/setOutputStream only concern the standard input/output of the command, nothing to do with port forwarding. get / setInputStream和get / setOutputStream仅涉及命令的标准输入/输出,与端口转发无关。 The port forwarding is done through two functions setPortForwardingL and setPortForwardingR. 端口转发通过两个函数setPortForwardingL和setPortForwardingR完成。

You should have something like that in your code. 您的代码中应该有类似的内容。

int assinged_port=session.setPortForwardingL(lport, rhost, rport);

As always, there is very few documentation on JSCH but a lot of detailed examples for L forwarding and R forwarding 与往常一样,关于JSCH的文档很少,但是有很多有关L转发R转发详细示例

The port forwarding is done on the session so I'm not sure you also need an active connection. 端口转发是在会话上完成的,因此我不确定您是否还需要活动连接。 But if you do, you should consider opening a 'shell' connection instead of an 'exec'. 但是,如果这样做,则应考虑打开“ shell”连接而不是“ exec”。 Then you wouldn't have to run a useless command just to maintain the connection. 这样,您就不必运行无用的命令来维护连接。

PS: Some code is missing so it's difficult to say something precise on the example given in the question. PS:缺少一些代码,因此很难在问题中给出的示例中说出准确的东西。

Thanks for your help. 谢谢你的帮助。 I solved it using Pipes, i share what i did: 我用Pipes解决了这个问题,我分享了自己的所作所为:

  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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM