使用ChannelExec的命令未执行-Jsch [英] Command using ChannelExec not executed - Jsch

查看:654
本文介绍了使用ChannelExec的命令未执行-Jsch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

在调用connect()之前,必须先调用getInputStream().

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

实际上,您最好同时阅读stderr和stdout以获取错误.
为此,请参阅我对如何读取JSch命令输出的答案?

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?

这篇关于使用ChannelExec的命令未执行-Jsch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆