Command using ChannelExec not executed - Jsch

0

I am using Jsch to create a file in the server and execute some commands. For the file creation, it's working fine, however, for the command execution, doesn't. It keeps the status -1 (still working on it) and keep there forever. This happen for shell execution or when I try to became root. Follow the method used below:

public void upload(String localPath) throws IOException {
    Session session = connectToServer();
    System.out.println("In upload");
    ChannelSftp channelSftp = getChannelToSftpServer(session);

    //Creating file in temporary location
    File f = new File(localPath);
    FileInputStream fi = new FileInputStream(f);

    // Creating file on server and setting the permissions to the user (chmod 777)

    if (channelSftp != null) {
        try {
            System.out.println("Change working in temp directory");
            changeWorkingDirectory(channelSftp, TEMP_PATH);
            
            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
            //THE PROBLEM ALSO HAPPENS WHEN EXECUTING A SHELL WITH THIS COMMAND INSIDE
            channelExec.setCommand(
                "root command (using pbrun) <command is here, confidential> "); 
            InputStream commandOutput = channelExec.getInputStream();
            channelExec.connect();

            StringBuilder outputBuffer = new StringBuilder();
            int readByte = commandOutput.read();

            while(readByte != 0xffffffff)
            {
               outputBuffer.append((char)readByte);
               readByte = commandOutput.read();
               System.out.println(outputBuffer);
            }
            System.out.println("Root connected.");
            channelExec.disconnect();
            
            channelSftp.put(fi, f.getName());
            channelSftp.chmod(0777, localPath);
            channelSftp.chown(123, localPath);
            channelSftp.chgrp(123, localPath);
            
            System.out.println("File configurations changed.");
            
            //Copying to the official path
            channelExec = (ChannelExec) session.openChannel("exec");
            channelExec.setCommand("mv /tmp/"+f.getName()+" "+path);
            channelExec.connect();
            System.out.println("File is completed and ready!");
            
            while (channelExec.getExitStatus() == -1) {
                Thread.sleep(1000);
                
            }
            channelExec.disconnect();

        } catch (SftpException e) {
            e.printStackTrace();
            throw new IOException(e.getStackTrace() + "");
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            disconnectChanneltoSftpServer(channelSftp);
            session.disconnect();
            fi.close();
            // Deletes the local File.
            f.delete();
        }
    }
}

What am I doing wrong? Thank you in advance.

java
ssh
jsch
asked on Stack Overflow Nov 28, 2017 by Julia Bel • edited Nov 2, 2020 by Martin Prikryl

1 Answer

1

You have to call getInputStream() before calling connect().

And you actually better read both stderr and stdout to get the errors.
For that, see my answer to How to read JSch command output?

answered on Stack Overflow Nov 29, 2017 by Martin Prikryl • edited Nov 12, 2020 by Martin Prikryl

User contributions licensed under CC BY-SA 3.0