Running command using “exec” channel with JSch does not return any output

后端 未结 1 1521
一向
一向 2020-12-10 08:50

I am trying to execute a command on a Linux server using SSH from Android with JSch.

As far as I know I am connecting to the server, but when I attempt to retrieve t

相关标签:
1条回答
  • 2020-12-10 09:05

    You disconnect immediately after you start the command, before any output is returned.

    You have to wait for the "exec" channel to close (it closes once the command finishes).

    See the official JSch example for the "exec" channel.

    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()){
        if(in.available()>0) continue; 
        System.out.println("exit-status: "+channel.getExitStatus());
        break;
      }
      try{Thread.sleep(1000);}catch(Exception ee){}
    }
    

    Though to allow the command to reliably complete and to collect all output including the errors, see How to read JSch command output?

    0 讨论(0)
提交回复
热议问题