0

I'm writing a SSH wheel based on JSch library. I'm use shell channel and setPty(false) in order to get more control on it and ensure response is clean message.A code usage simple as this is enough for the topic:

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class JShellTest {
    public static void main(String[] arg){
        try{
            JSch jsch=new JSch();

            //jsch.setKnownHosts("/home/foo/.ssh/known_hosts");
            Session session=jsch.getSession("lorancechen", "192.168.1.149", 22);

            session.setPassword(" ");
            session.setConfig("StrictHostKeyChecking", "no");

            //session.connect();
            session.connect(30000);   // making a connection with timeout.

            Channel channel=session.openChannel("shell");

            // Enable agent-forwarding.
            ((ChannelShell)channel).setPty(false);
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);

            channel.connect(60*1000);
        }
        catch(Exception e){
            System.out.println(e);
        }

    }

}

What's problem here?
When I input lll, it's a not allowed command, the result response nothing.
Compare with ssh -T foo@bar, it will be response -bash: line 2: lll: command not found.

So, how can I let JSch response the same result as ssh -T foo@bar, if the command fail?
Thanks.

LoranceChen
  • 2,453
  • 2
  • 22
  • 48
  • hi,I'm use channel. Not exec. – LoranceChen Aug 21 '17 at 14:29
  • "exec" is also "channel". You probably mean that you use "shell channel". First, you should not. The "shell" channel is not intended for automating a command execution. It's for implementing an interactive shell session. And even, if you have to use "shell" channel for whatever reason, the solution is the same. You just have to use `Channel.setExtOutputStream` directly (what `ChannelExec.setErrStream` calls internally). – Martin Prikryl Aug 21 '17 at 14:34
  • @MartinPrikryl, thanks.I think I will try `setExtOutputStream ` because I just need the error message to compile the wheel. I suddenly realized I'm just implement a `exec` channel based on `shell` channel, the wheel seems go too far.>. – LoranceChen Aug 21 '17 at 14:38

0 Answers0