1

I have successfully SSH'ed into a node, sent the input, and retrieved the output. After inputting a line, the line is printed to the console, followed by a blank line, and then the output prints twice. I don't want the input to print to the console after it is entered, nor the blank line, nor the output printed a second time. Below is the code I have

public void runSession() {
    try {
        Channel channel = session.openChannel("shell");
        channel.setInputStream(System.in, true);
        channel.setOutputStream(System.out, true);
        channel.connect(defaultChannelTimeout);

        while (channel.getExitStatus() == -1) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }

        channel.disconnect();
    } catch(JSchException jschEx) {
        System.out.println("JSch exception during I/O");
        System.out.println(jschEx.getMessage());
    }
}

Here is what the console looks like when running

user:domain@node:/a/b/c> cd ..

cd ..

user:domain@node:/a/b> user:domain@node:/a/b>

As you can see, there are issues:

  • The "cd.." is printed on a line to the console by itself
  • A blank line appears after the "cd.."
  • The "user:domain@node:/a/b>" line is printed twice.

Does anyone know how I can remove these 3 items from being displayed in the console? Desired output is

user:domain@node:/a/b/c> cd..

user:domain@node:/a/b>

4
  • What are you actually implementing? Why are you using the "shell" channel? Commented Oct 20, 2016 at 6:24
  • I need to keep the channel open to log into another program from the shell that allows users to query/insert/update data in a database. Using "exec" will close the channel after each command and the user would never be able to login (takes 3 inputs to login). Commented Oct 20, 2016 at 15:36
  • What three inputs? Do you mean you need to execute three commands in a sequence? Commented Oct 21, 2016 at 6:06
  • Yes. The program prompts the user for username, domain, and password. Executing one command and closing the channel won't work. Commented Oct 21, 2016 at 18:23

1 Answer 1

1

These are all consequences of the "shell" channel.

You run an interactive session with all the fancy stuff that human users like.

The "shell" channel is not intended for automation.

You may remove some of these by calling channel.setPty(false) before channel.connect().


Though you better use the "exec" channel.

This may work:

( echo username & echo password & echo hostname ) | command

Related questions:

Sign up to request clarification or add additional context in comments.

3 Comments

No luck. Also, I'm not looking for automation. I want the user to be able to input their own information, queries, etc. which is why I chose to implement the 'shell' channel as opposed to the 'exec' channel.
"No luck" with what? The setPty or the "exec" channel"? The approach with "exec" channel does not prevent you from prompting the user for the information. Just prompt for the information and assemble the command using the entered data.
Using setPty, no output was printed to the console (did not receive a prompt for "user:domain@node:/a/b>"). Using the exec channel, the user is unable to provide input to execute multiple commands unless I previously prompt for the commands, store them in a list, and execute each command stored in the list. That is not at all what I want to implement. I want the channel to remain open and the channel itself to accept user input.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.