I’m using Primefaces’ Terminal component and JSch to ssh to a remote desktop. With the exec channel the execution takes too much time as both session and channel closes at each command and I didn’t manage to avaid that. So I changed the channel to shell and now I’m trying to “redirect” standard input/outputsteam. Here’s what my code looks like :
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
@ManagedBean
@SessionScoped
public class TerminalController implements Serializable{
public TerminalController(){
jsch=new JSch();
InputStream in=null;
PrintStream out=System.out;
try{
session=jsch.getSession(user, ip, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(passwd);
session.connect();
channel=session.openChannel("shell");
channel.setInputStream(in);
channel.setOutputStream(out);
channel.connect();
}catch(Exception ee){
System.out.println(ee);
} }
public String handleCommand(String command, String[] params) {
command=command+StringUtils.join(params," ");
in=IOUtils.toInputStream(command);
String result=out.toString();
out.flush();
return result;}
I know it’s a mess, I’m still a beginner in java.
Another problem I thought about is that in the conversion from iostream to string I may lose the enter button function ! I’m waiting for you suggestions, solutions and pieces of advice.
Setting the
invariable has no effect on the channel, and callingout.toString()will not get you any results.You need to write the command (with its parameters) to the channel, and then read the output from there. Also, don’t use the
setInputStreamorsetOutputStreammethods (they are good if you have existing streams from which to read or to which to write, which you don’t).The hard part is actually knowing when the output from the remote command is finished, and you need to return (this is in the
looksLikePromptmethod I don’t know how to write).I’m not sure that the
CommandHandlerprinciple of Primeface’s terminal component is the right design for creating an interactive terminal, where the terminal shouldn’t need to know when a command is finished. What happens when a command needs more input?