使用ChannelExec的命令未执行 - Jsch

时间:2017-11-28 19:37:20

标签: java ssh jsch

我正在使用Jsch在服务器中创建一个文件并执行一些命令。对于文件创建,它可以正常工作,但是,对于命令执行,它没有。它保持状态-1(仍在使用它)并永远保持在那里。这种情况发生在shell执行或我尝试成为root时。请遵循以下方法:

   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");
                    channelExec.setCommand("root command (using pbrun) <command is here, confidential> "); //THE PROBLEM ALSO HAPPENS WHEN EXECUTING A SHELL WITH THIS COMMAND INSIDE
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();

                }
            }
        }

我做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:1)

在致电getInputStream()之前,您必须致电connect()

你实际上更好地阅读stderr和stdout来获取错误 为此,请参阅我对How to read JSch command output?

的回答