Command using ChannelExec not executed - Jsch

谁都会走 提交于 2020-11-29 03:55:04

问题


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.


回答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?



来源:https://stackoverflow.com/questions/47539582/command-using-channelexec-not-executed-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!