title: JSch exec sudo command : How to access sudo command output text?
body: <p>I am using JSch to run a sudo command on a remote linux server.</p>
  
  <p>The example below shows an attempt to cd into a directory and return the contents. I am testing the use case where <code>myDirectory</code> does NOT exist.</p>
  
  <pre><code>public static List&lt;String&gt; executeExecCommand() {
  
    try {
  
      Session session = new JSch().getSession() // paraphrasing for brevity
  
      Channel channel - session.openChannel("exec");
  
      String command = "echo \"password\" | sudo -S bash -c \"cd myDirectory/ &amp;&amp; ls -la \"";
  
      ((ChannelExec(channel).setCommand(command);
      channel.setInputStream(null);
      ((ChannelExec(channel).setErrStream(System.err);
      InputStream input = channel.getInputStream();
      channel.connect();
      List&lt;String&gt; output = new ArrayList&lt;&gt;();
  
      try {
          InputStreamReader inputReader = new InputStreamReader(input);
          BufferedReader bufferedReader = new BufferedReader(InputReader);
          String line = null;
          while (true) {
              while ((line = bufferedReader.readLine()) != null) {
                  output.add(line);
                  System.out.println("here is a line " + line);
              }
          if (channel.isClosed()( {
              if (input.available() &gt; 0) {
                  continue;
              }
              logger.DEBUG("exit status " + channel.getExitStatus());
              break;
          }
          bufferedReader.close();
          inputReader.close();
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  
      channel.disconnect();
      session.disconnect();
  
      return output;
  
      } catch (Throwable t) {
          throw new JSchException("executeExecCommand : unable to establish SSH session " + t.toString());
      }
  }
  </code></pre>
  
  <p>When run from a Unit Test in intelliJ I get output as follows:</p>
  
  <pre><code>.
  .
  [sudo] password for username : bash: line 0: cd: myDirectory/: No such file or directory
  
  2019-01-03 10:40:18 DEBUG [main] &lt;Filename&gt;:&lt;linenumber&gt; - exit status: 1
  .
  .
  </code></pre>
  
  <p>This is to be expected, but my question is How do I programmatically access the [sudo] output line? I have looked at the ErrorStream but that does not contain the text which is output and displayed on intelliJ's output console.</p>
  
  <p>Note the line <code>System.out.println("here is a line " + line);</code> is not executed.</p>
  
  <p>Note also that that the [sudo] line is not output when run from command prompt : ie "mvn test"</p>
  
  <p>So, how do I programmatically access the [sudo] output line?</p>