-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..

  • I think that a good start is using ProcessBuilder https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html Also you can find few examples here: https://stackoverflow.com/questions/6856028/difference-between-processbuilder-and-runtime-exec#6856659 – Daniel C. Aug 18 '17 at 03:14
  • Yes thanks for the quick answer much appreciated, I was also looking into that but ultimately more complicated than using paramiko in python and making a rabbit mq interface between my java process and a (python) ssh console. – Samuel Audet Arsenault Aug 18 '17 at 12:08

1 Answers1

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();
    }

}

}

  • 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. – Samuel Audet Arsenault Aug 18 '17 at 16:38