Hi Bros/Sis,                
       
       I use the jsch to execute interactive commands(that prompt for some 
input/password) on the remote hosts which work perfectly fine.
       There are some interactive commands which require a terminal for 
execution eg. sudo/su.
       For these commands I set the pty flag to true ie 
"((ChannelExec)channel).setPty(true)" 
       This is also working perfectly fine, but for some instances  the 
execution hangs, the reason being the input/password is being piped even before 
the pseudo terminal is launched
       and hence the command is waiting infinitely for the password/input.

       The following is the code snippet used .
        
======================================================================================================================================
       //Set the pseudo terminal allocation flag to true
        ((ChannelExec)channel).setPty(true);
        
        // Set the input stream with the input to be passed for the remote 
command 
        byte[] cmdbuf = cmdInput.getBytes();
        ByteArrayInputStream bi = new ByteArrayInputStream(cmdbuf);
           channel.setInputStream(bi);
        
        //Set the channel's error stream
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ((ChannelExec)channel).setErrStream(bo);
      
      //Get the input stream of the channel   
       InputStream in=channel.getInputStream();

       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));
          //Hang is detected since the password is pumped in even before the 
pseudo terminal is launched and now the command(sudo/su) is waiting infinitely 
for the password.
      }
        if(channel.isClosed()){
          System.out.println("exit-status: "+channel.getExitStatus());
          break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){}
      }
      channel.disconnect();
      session.disconnect();
    
======================================================================================================================================
    
    How do I go about resolving this issue.
    
    i. Is there a way to introduce a delay or ensure that the pseudo terminal 
and the command is launched and only then the input/password is piped in.
    ii. If the above option is not feasible, is there a way to detect this hang 
and once again set the input/password in the InputStream.
    
    
    thanks,
    Prakash


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to