Sending commands to server via JSch shell channel

后端 未结 9 973
渐次进展
渐次进展 2020-11-30 03:37

I can\'t figure it out how I can send commands via JSch shell channel.

I do this, but it doesn\'t work:

JSch shell = new JSch();
String command = \"c         


        
9条回答
  •  醉酒成梦
    2020-11-30 03:45

    try this

    Channel channel=session.openChannel("shell");
                OutputStream ops = channel.getOutputStream();
            PrintStream ps = new PrintStream(ops, true);
    
             channel.connect();
             ps.println("mkdir folder"); 
             ps.println("dir");
     //give commands to be executed inside println.and can have any no of commands sent.
                          ps.close();
    
             InputStream in=channel.getInputStream();
             byte[] bt=new byte[1024];
    
    
             while(true)
             {
    
             while(in.available()>0)
             {
             int i=in.read(bt, 0, 1024);
             if(i<0)
              break;
                String str=new String(bt, 0, i);
              //displays the output of the command executed.
                System.out.print(str);
    
    
             }
             if(channel.isClosed())
             {
    
                 break;
            }
             Thread.sleep(1000);
             channel.disconnect();
             session.disconnect();   
             }
    

提交回复
热议问题