-1

If someone have an actual solution to this problem i would much appreciate it. So far all implementation that I have used close the session as soon as one of the channel is "connected" what ever that means. Like most i need to be able to script ssh interaction meaning that i need the result of my operation with a still alive channel I'm not looking for a command with "cmd1;cmd2;cmd3" type ..

The best example I can think of is if you were trying to browse trough a file system.

If each command is a new session you would be going going no where since at each new session you go back to square one.

In command line the ssh session remain open when you type an operation why all java implementation differ so much from this approach is beyond me. My next step if i cant find an answer is actually to use command shell from java and interacting from there instead of using java ssh libraries..

2

1 Answer 1

0
  public void connect() {
    Session session;
    try {
        session = createConnectedSession();
        java.util.logging.Logger.getLogger("test").log(Level.INFO,"isConnected "+session.isConnected());
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Channel channel = session.openChannel("shell");

        channel.setOutputStream(output);

        PrintStream ps = new PrintStream(channel.getOutputStream(), true);
        // InputStream is = new InputStream(channel.getInputStream());
        channel.connect();
        sleep();
        java.util.logging.Logger.getLogger("test").log(Level.INFO,"isConnected "+session.isConnected());
        Stack<String> mstack = getCommandStack();
        //readChannel(channel);
        while (!mstack.isEmpty()) {
            String cmd = mstack.pop();
            java.util.logging.Logger.getLogger("test").log(Level.INFO,"sending command "+cmd);
            ps.println(cmd);
            sleep();
            System.out.println(output.toString());
            java.util.logging.Logger.getLogger("test").log(Level.INFO,"command result"+output.toString());
            sleep();
            // System.out.println(output.toString());
            ps.flush();
        }

        channel.disconnect();
        session.disconnect();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

1
  • That actually worked for me, had several issue and inconsistent results, but now it seems to work fine I'm able to send multiple command within a channel without having it closing on me directly. Still have not figured if that was a bug or a real limitation since many like me were looking for the same answer here it is it is possible to send multiple command and read the output with JSCh i tested it and it work. Commented Aug 18, 2017 at 16:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.