Jsch退出状态始终为-1

时间:2016-01-29 15:33:55

标签: java ssh jsch

在这里遇到问题并希望有人可以提供帮助:

    public void doSomeStuff(){

    JSch sConn = new JSch();
    try {
        sConn.addIdentity("/path/to/privatekey","/path/to/public/key",null);
    } catch (JSchException e) {
        e.printStackTrace();
    }

    Session session = null;
    try {
        session = sConn.getSession("userid", "localhost", 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
    } catch (JSchException e) {
        e.printStackTrace();
    }



    ChannelExec channel = null;
    try {
        channel = (ChannelExec) session.openChannel("exec");
        BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));

        channel.setCommand("ls -la;");
        channel.connect();

        String msg=null;
        while((msg=in.readLine())!=null){
          System.out.println(msg);
        }

    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(channel.getExitStatus());
    channel.disconnect();
    System.out.println(channel.isClosed());
    System.out.println(channel.getExitStatus());
    session.disconnect();
    System.out.println(channel.getExitStatus());}
在这种情况下,

channel.getExitStatus()总是返回-1,尽管ls -la返回预期的输出。在http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/Channel.html#getExitStatus%28%29读取文档告诉我必须关闭通道,以便它可以返回退出状态,否则返回-1,但在我的情况下,我总是得到-1。

任何人都知道在这种情况下问题是什么?

1 个答案:

答案 0 :(得分:0)

在JCraft的示例中使用代码我重新构建了处理输入流并使用以下代码:

public void doSomeStuff(){

    JSch sConn = new JSch();
    try {
        sConn.addIdentity("/home/user/.ssh/id_rsa","/home/user/.ssh/id_rsa.pub",null);
    } catch (JSchException e) {
        e.printStackTrace();
    }

    Session session = null;
    try {
        session = sConn.getSession("user", "localhost", 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    ChannelExec channel = null;
    try {
        channel = (ChannelExec) session.openChannel("exec");


        channel.setCommand("lsa;");
          ((ChannelExec)channel).setCommand("ls -la");


          InputStream in = channel.getInputStream();
          ((ChannelExec)channel).setErrStream(System.err);

          channel.connect();


          byte[] tmp=new byte[1024];
          while(true){
            while(in.available()>0){
              int i=in.read(tmp, 0, 1024);
              if(i<0)break;
              System.out.print(new String(tmp, 0, i));
            }
            if(channel.isClosed()){
              System.out.println("exit-status: "+channel.getExitStatus());
              break;
            }
            try{Thread.sleep(1000);}catch(Exception ee){}
          }
          channel.disconnect();
          session.disconnect();
        }
        catch(Exception e){
          System.out.println(e);
        }

    channel.disconnect();
    session.disconnect();
    System.out.println(channel.getExitStatus());

}