title: Java program hangs after executing sudo su command?
body: <p>I have a program that can <code>ssh</code> into a remote host and remotely execute commands after that. Commands like <code>mkdir</code> and <code>cd</code> work but when I try to execute the command <code>sudo su - username</code> the program just hangs. I was wondering if there's anything missing/wrong in my code. </p>
  
  <pre><code>JSch jSch = new JSch();
  Channel channel = null;
  Session session = null;
  InputStream in = null;        
  String  username;
  OutputStream  os  = null;;
  
  try {   
      Properties conf = new Properties();
      conf.put("StrictHostKeyChecking", "no");
  
      jSch.addIdentity("id_rsa");
      jSch.setConfig(conf);
      session = jSch.getSession("username", "hostname", 22);      
  
      String cmd = "mkdir test";
      session.connect();   //   creating the ssh connection        
  
      channel = (ChannelExec) session.openChannel("exec");        
      ((ChannelExec)channel).setCommand(cmd);
      channel.setInputStream(null);
      in = channel.getInputStream(null);        
      channel.connect();  
  
      byte[] tmp = new byte[1024];        
      while (true) { 
          while (in.available() &gt; 0) {
              int i = in.read(tmp, 0, 1024);
              if (i &lt; 0) {
                  break;
              }
          }
          if (channel.isClosed()) {
              break;
          }      
          try {
              Thread.sleep(1000);  // to wait for long running process ..                
          } catch (Exception ee) {
          }
          String value = new String(tmp);            
          System.out.println("input stream " + value);
      }     
  }catch(Exception  e){
      e.printStackTrace();
  }finally{
      channel.disconnect();
      session.disconnect();
      if(in!=null)
      in.close();
  }
  </code></pre>
  
  <p>Also, I need to <code>ssh</code> from this host to another host after I <code>sudo</code>, so basically I need to <code>ssh</code> to a remote host via a gateway kind of a thing and then connect to a database, once this problem gets fixed.</p>
  
  <p>Any light on this will be greatly appreciated.</p>
  
  <p>Thanks.</p>