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>

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
David
  • 11
  • 3
  • What are you actually implementing? Why are you using the "shell" channel? – Martin Prikryl Oct 20 '16 at 06: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). – David Oct 20 '16 at 15:36
  • What three inputs? Do you mean you need to execute three commands in a sequence? – Martin Prikryl Oct 21 '16 at 06:06
  • Yes. The program prompts the user for username, domain, and password. Executing one command and closing the channel won't work. – David Oct 21 '16 at 18:23

1 Answers1

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:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 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. – David Oct 21 '16 at 21:22
  • "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. – Martin Prikryl Oct 22 '16 at 05:43
  • 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. – David Oct 24 '16 at 22:37