title: Jsch - Sending commands via ssh
body: <p>The problem: I cant seem to get out of the &quot;infinite loop/block&quot; from the buffered reader.readline(). It passes it once and then just hangs there after the second come around from the while loop.</p>
  <p>Here is my code:</p>
  <pre><code>public void sendCommand(String command){
          {
              String line;
               try{
                   while((line = consoleOutput.readLine())!= null)
                      {
                           System.out.println(line);
                           if(line.equals(&quot;rkscli:&quot;) || line.equals(&quot;password :&quot;) || line.equals(&quot;Please login: &quot;)){
                               pin.write((command + &quot;\n\r&quot;).getBytes());
                           }
                      }
                   }
  </code></pre>
  <p>I am using the method used by nilbot from: <a href="https://stackoverflow.com/questions/4194439/sending-commands-to-server-via-jsch-shell-channel">Here</a>
  I am doing exactly this, where I am trying to send commands via a shell.</p>
  <p>The Problem:</p>
  <p>What I am trying to accomplish is getting the program to wait until the server is ready to receive more commands (wait for the prompt &quot;rkscli&quot;, because some commands take more time). What can I do to accomplish this? I am currently using the pipedinput/output method.</p>
  <p>Other info:</p>
  <p>The server will give me &quot;&quot;'s (empty strings with newlines) before the prompt. After a few of those it will give me prompt. I also noticed that if I changed the code so that it looked like this:</p>
  <pre><code>public void sendCommand(String command){
      {
          String line;
           try{
               while((line = consoleOutput.readLine())!= null)
                  {
                       System.out.println(line);
                                       pin.write(&quot;\r\n&quot;.getBytes());//Added line
                       if(line.equals(&quot;rkscli:&quot;) || line.equals(&quot;password :&quot;) || line.equals(&quot;Please login: &quot;)){
                           pin.write((command + &quot;\n\r&quot;).getBytes());
                       }
                  }
               }
  </code></pre>
  <p>It will not hang at the bufferedReader.readline() but will go through and skip the write (It does not seem to write anything to the shell and sometimes it will and is not predictable)</p>
  <p>Any ideas as to what I can do?</p>