Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:16:41+00:00 2026-05-16T06:16:41+00:00

Goal: Remote control ssh server with one connection and multiple sessions or one persistent

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:16:41+00:00Added an answer on May 16, 2026 at 6:16 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Goal: use webdeploy to deploy builds from TeamCity to remote IIS server I
My goal is to get the list of web site names from remote server.
Goal: Start up a server which supports remote access to method calls. The application
My goal is to open a new terminal window, SSH into a remote machine
The goal: Remote control of an Android device. I'm trying to make an alternate
Goal: Once i click on the start button on my user interface, i currently
What's a good approach to revision control PGP encrypted text files? The goal is
I have the following remote form_tag, whose goal is to POST the following params:
Goal: Add a new file to a remote git repository without checking the whole
I'm trying to loggin to remote server with login and password and it's done.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.