1

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.

1 Answer 1

1

Question 1: can sshj be used to "expect" password prompts? I couldnt find any documentation alluding to that type of control.

Mainly what sshj provides is a handle on the shell or command's I/O streams, and methods to get stuff like the exit status.

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.

Multiplexing sessions, even concurrent session over a single SSH connection is supported. But note that you can only have one session for one shell/command/subsystem.

Which kind of Session are you using, i.e. Session.Shell (via session.startShell) or Session.Command (via session.exec)? In the latter case, once you're done executing a command, the session is meant to close and this is the expected behavior.

Normally wouldn't recommend Shell over Command but since you are equipped with ExpectJ which I guess can deal with prompts and such, you might want to go that route. If there are problems with echoing of characters or such, play with the PTY options (instead of session.allocateDefaultPTY call session.allocatePTY which takes a whole bunch of args, have a look at the source for allocateDefaultPTY).

Code examples: Shell, Command

Also, if you narrow the problem down to a bug with sshj, do report it on the issue tracker :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.