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.