【问题标题】:Jsch exit status always -1Jsch退出状态总是-1
【发布时间】:2016-01-30 01:42:31
【问题描述】:

这里有问题,希望有人能提供帮助:

    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。

有人知道这种情况下的问题是什么吗?

【问题讨论】:

    标签: java ssh jsch


    【解决方案1】:

    使用 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());
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-12
      • 2018-04-11
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2021-05-31
      相关资源
      最近更新 更多