Goal: Remote control ssh server with one connection and multiple sessions or one persistent session.
Issue 1: I currently use sshj to do some remote control through SSH and it works well but I cant seem to get it to handle prompts correctly. (the host doesnt provide true root, just sudo -i so I need to log in first). Issue 2: I downloaded ExpectJ to handle the prompt but I can't for the life of me figure out how to maintain a session once I have logged in and authenticated as root.
The current Hack solution requires that I re-log in every time:
public class Expect {
Spawn shell;
ExpectJ exp;
String host;
int port;
String username;
String passwd;
boolean sudo = false;
public Expect(String host,int port,String username,String passwd) throws IOException, TimeoutException, ExpectJException{
exp = new ExpectJ(5);
this.host = host;
this.port = port;
this.username = username;
this.passwd = passwd;
shell = exp.spawn(host, port, username, passwd);
shell.send("sudo netstat -natvp | grep Xtightvnc\n");
System.out.println(shell.getCurrentStandardOutContents());
try{
shell.expect("[sudo] password for #######:");
shell.send(passwd+"\n");
}
catch (IOException ex){
String err = ex.toString();
if(!err.equals("java.io.IOException: End of stream reached, no match found")){
throw new IOException(ex);
}
}
}
Question 1: can sshj be used to "expect" password prompts? I couldnt find any documentation alluding to that type of control.
Quetsion 2: How can I modify the above Expect code to maintain a persistent connection that I can make multiple calls to? I want to be able to continue to interact once I have reached the state of authenticating as root but the Spawn always closes once the initial command has been sent.