title: Java Jsch sudo as user and execute a script
body: <p>Using jsch i have login to the remote host execute the script as different user.
  Have to use "exec" channel.
  current unix action i do is:
     1) sudo su - 
     2) run script</p>
  
  <p>How can i run this command  "sudo su - " and then execute the script in the same channel</p>
  
  <hr>
  
  <p>Updated code comments and i am trying to send below two command as input. it is running in loop and i dont see it is getting executed. the below tw are the input sent in the array list "commands"
  sudo su - testusr
  /home/testusr/start.sh</p>
  
  <pre><code>ChannelShell channel = null;
  
  List&lt;String&gt; result = new ArrayList&lt;String&gt;();
  InputStream inStream = null;
  OutputStream outStream = null;
  
  PipedOutputStream pOutStream = null;
  PipedInputStream pInStream = null;
  try {
      inStream = new PipedInputStream();
      pOutStream = new PipedOutputStream((PipedInputStream) inStream);
  
      outStream = new PipedOutputStream();
      pInStream = new PipedInputStream((PipedOutputStream) outStream);
      channel = (ChannelShell) session.openChannel("shell");
      // channel.setPty(true);
      channel.setInputStream(inStream);
      channel.setOutputStream(outStream);
      channel.connect();
      BufferedReader bfs = null;
  
      for (String command : commands) {
          LOGGER.info("Executing command {} ", command);
          pOutStream.write((command.concat("\n")).getBytes());
      }
      LOGGER.info(" exit status {}", channel.getExitStatus());
      bfs = new BufferedReader(new InputStreamReader((inStream)));
      if (channel.getExitStatus() != 0) {
  
          result.add("ERROR");
      }
      String line;
      byte[] bt = new byte[1024];
      while (true) {
          while (inStream.available() &gt; 0) {
              int i = inStream.read(bt, 0, 1024);
              if (i &lt; 0) {
                  break;
              }
              LOGGER.info("result {}", new String(bt, 0, i));
          }
          if (channel.isClosed()) {
              LOGGER.info("exit status {}", channel.getExitStatus());
              break;
          }
          try {
              Thread.sleep(1000);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <hr>
  
  <p>2ned EDIT</p>
  
  <pre><code>for (String command : commands) {
                  OutputStream out = channel.getOutputStream();
                  out.write((command.concat("\n")).getBytes());
                  out.flush();
                  InputStream in = channel.getInputStream();
                  byte[] tmp = new byte[1024];
                  while (true) {
                      while (in.available() &gt; 0) {
                          int i = in.read(tmp, 0, 1024);
                          if (i &lt; 0) {
                              break;
                          }
                          LOGGER.info("Output stream execution {}", new String(
                                  tmp, 0, i));
                      }
                      if (channel.isClosed()) {
                          LOGGER.info("Executing exit status {}",
                                  channel.getExitStatus());
                          break;
                      }
                  }
              }
  </code></pre>