Question

I can successfully connect to my remote ssh server using the jsch library and fire a single command and see its result. I am using "exec" to send the 'ls' command and able to see the list of files. Is there a way to open a shell prompt so that i can fire multiple command to the server.

JSch jsch=new JSch();
Properties props = new Properties(); 
props.put("StrictHostKeyChecking", "no");

Session session;
try {

session = jsch.getSession(user, host,22);
session.setConfig(props);
session.setPassword(password);
session.connect(20000);
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("ls");
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect(20000); 
Was it helpful?

Solution

See my answer here, it includes a link to the official JSCH example for creating an interative ssh session with jsch. I also posted an improvement to their program, which allows it to properly close the session when you exit.

Never ending of reading server response using jSch

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top