title: Running Command on a Router Line By Line (JSCH API)
body: <p>I have written a method in Java, which takes an ArrayList of Strings and tries to run the command on a CISCO router console one by one. And I am trying to print the output of each command on the console. I am using the Jsch API for it.</p>
  <p>I am able to connect to the router just fine using SSH. But when I print the <code>OutputStream</code> to the console it just prints the same thing over and over again. No output of the commands I sent. I intentionally have a few incorrect commands in there so that the console prints the error, but I get no such thing.</p>
  <p>Here is my method:</p>
  <pre><code>/**
   * Deliver a config to the router line by line.
   * @param configLines
   * @throws DeviceExcpetion
   */
  public void deliverConfig(List&lt;String&gt; configLines) throws DeviceExcpetion {
  
      Channel channel = null;
      JSch jSch = new JSch();
      Session session = null;
  
      try {
  
          session = jSch.getSession(userid, ipAddress, port);
  
          // Set the password.
          session.setPassword(password);
  
          session.setConfig(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
  
          // Finally make the connection.
          session.connect();
  
          channel = session.openChannel(&quot;shell&quot;);
  
          InputStream inputStream = null;
  
          ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
  
          channel.setOutputStream(responseStream);
  
          channel.connect();
  
          for (String line : configLines) {
  
              inputStream = new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8));
  
              channel.setInputStream(inputStream);
  
              if (channel.isConnected()) {
                  Thread.sleep(1000);
              }
  
              String responseString = new String(responseStream.toByteArray());
  
              System.out.println(responseString);
          }
  
      } catch (JSchException e) {
  
          e.printStackTrace();
  
          throw new DeviceExcpetion(e.getMessage());
  
      } catch (InterruptedException e) {
  
          e.printStackTrace();
  
          throw new DeviceExcpetion(e.getMessage());
  
      } finally {
  
          if (session != null) {
              session.disconnect();
          }
  
          if (channel != null) {
              channel.disconnect();
          }
      }
  }
  </code></pre>