SSH [SOLVED]: Why IOException is raised after invoking Session::execCommand() is raised?

SSH [SOLVED]: Why IOException is raised after invoking Session::execCommand() is raised?

Home Forums SSH SSH [SOLVED]: Why IOException is raised after invoking Session::execCommand() is raised?

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36060

    Anonymous

    QuestionQuestion

    I’m trying to run a service on a remote machine using SSH protocol. I use Ganymed SSH-2 for Java library (version 262) and for executing a single command it works fine, but I’ve faced some difficulties while trying to execute several commands during the single session.

    Could you help me to figure out why IOException is raised and how could I fix it?

    Please, find the code in the excerpt below:

    private static void executeShellCommands(Connection connection, List<String> commandList) throws Exception {
        Session session = connection.openSession();
        InputStream stdout = new StreamGobbler(session.getStdout());
        session.requestDumbPTY();
        session.startShell();
        for(String command : commandList) {
            // The next line throws java.io.IOException
            session.execCommand(command); 
            try (BufferedReader br = new BufferedReader(new InputStreamReader(stdout))) {
                String line = br.readLine() + "n";
                StringBuilder shellOutput = new StringBuilder();
                while (line != null) {
                    shellOutput.append(line);
                    line = br.readLine() + "n";
                }
            }
        }
        session.close();
    }
    

    And the stacktrace is:

    java.io.IOException: A remote execution has already started.
    
    at ch.ethz.ssh2.Session.execCommand(Session.java:282)
    at ch.ethz.ssh2.Session.execCommand(Session.java:260)
    at com.myproject.test.ssh.util.SshOperations.executeShellCommands(SshOperations.java:124)
    at com.myproject.test.ssh.util.SshOperations.runBatchProcessingService(SshOperations.java:147)
    at com.myproject.test.ssh.step.definitions.DocumentBatchProcessingStepDefs.testFileIsCopiedToBpsViaSSH(DocumentBatchProcessingStepDefs.java:97)
    at ?.Given test file is copied to BPS via SSH (myrepo/src/test/resources/cucumber/Regression.feature:21)
    

    Thank you in advance.

    #36061

    Anonymous

    Accepted AnswerAnswer

    It is mentioned in Javadoc of ch.ethz.ssh2.Session

    A Session is a remote execution of a program. “Program” means in this context either a shell, an application or a system command […. ] Only one single program can be started on a session. However, multiple sessions can be active simultaneously.

    I think you should use multiple sessions, for example like this:

    private static void executeShellCommands(Connection connection, List<String> commandList) throws Exception {
        for(String command : commandList) {
            Session session = connection.openSession();
            InputStream stdout = new StreamGobbler(session.getStdout());
            session.requestDumbPTY();
            session.startShell();
            session.execCommand(command); 
            try (BufferedReader br = new BufferedReader(new InputStreamReader(stdout))) {
                String line = br.readLine() + "n";
                StringBuilder shellOutput = new StringBuilder();
                while (line != null) {
                    shellOutput.append(line);
                    line = br.readLine() + "n";
                }
            }
            session.close();
        }
    }
    

    Source: https://stackoverflow.com/questions/47811402/why-ioexception-is-raised-after-invoking-sessionexeccommand-is-raised
    Author: Würgspaß
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.