4

I am new to JSch and I have a problem with some of my script I try to execute remotely and which seems to never end (and does not do the same thing as when I run it using putty).

I have redirected error and output stream to my System.out and see indeed error when the script is executed but the script is finished! Therefore I don't understand why the channel is still open (isClosed and isEOF are false).

When I run the command when connecting in SSH with putty the script execute correctly and does not show any errors. When I do ssh user@host "my command" using ssh command in Ubuntu I get the same output (std + err) as when I use JSch but the ssh command does not hangs!

Do you have any idea of what I do wrong, why does I have different output/behavior? Here is the java code I run (by the way I CAN'T send several command with different channel on the same sessions and I have no idea why, I therefore open one session for each cmd).

public static void runCommand(String user, String password, String cmd) throws JSchException, IOException{
    Session session = jSsh.getSession(user, SERVER, SSH_PORT);
    session.setPassword(password);
    session.setConfig(SSH_PROPERTIES);
    session.connect();
    SshCommand sshCmd = new SshCommand(session, cmd);
    runCommand(sshCmd);
    session.disconnect();
}

private static void runCommand(SshCommand sshCmd) throws IOException, JSchException{

    Session session = sshCmd.getSshSession();
    String cmd = sshCmd.getCmd();


    UtilityLogger.log(Level.FINE, "Running command on ssh : "+cmd);

    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    channel.setCommand(cmd);
    channel.setInputStream(null);

    InputStream in = channel.getInputStream();
    InputStream err = channel.getErrStream();
    UtilityLogger.log(Level.FINEST, "Connecting to channel");
    channel.connect();
    UtilityLogger.log(Level.FINEST, "Channel connected");

    byte[] tmp = new byte[1024];
            byte[] tmp2 = new byte[1024];
    while (true) {
        //Flush channel
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0)
                break;
            UtilityLogger.log(Level.FINE, new String(tmp, 0, i));
        }
        //Flush Error stream
        while (err.available() > 0) {
            int i = err.read(tmp2, 0, 1024);
            if (i < 0)
                break;
            UtilityLogger.log(Level.FINE, new String(tmp2, 0, i));
        }
        if(DONT_WAIT_PROCESS_END)
            break;
        if (channel.isEOF()) {
            UtilityLogger.log(Level.FINE, "Channel exit-status: " + channel.getExitStatus());
            break;
        }
    }
    try{Thread.sleep(TIME_BETWEEN_COMMAND);}catch(Exception ee){}
    channel.disconnect();
    UtilityLogger.log(Level.FINEST, "Channel disconnected");
}
Abbadon
  • 2,513
  • 3
  • 25
  • 33
  • what happens if in.available() <= 0 and channel.isEOF() == false? Could be an endless loop problem. – oers Sep 28 '12 at 11:44

2 Answers2

2

Try appending "exit;" after your commands even while using exec channels.

Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81
2

Our app did not receive an EOF on the exec, too. Appending an exit; to the command did not solve the problem.

It had something to do with the stderr output. Redirecting stderr to stdout solved (workarounded?!) the problem.
So we appended 2>&1 to the command:

${command} 2>&1
Sven Döring
  • 3,927
  • 2
  • 14
  • 17
  • A command could execute ok but also can write to the stderr. If that the case the ssh channel will NOT send eof until stderr data is read. – bruno bb Sep 04 '20 at 11:55