title: JSch: How to keep the session alive and up
body: <p>I'm writing Java GUI program for static route management using SSH. My code is as follows:</p>
  
  <pre><code>import com.jcraft.jsch.*;
  import java.io.*;
  
  public class Konsep {
      String status;
      static String username;
      static String hostname;
      String inputcommand;
      String output;
      static Session session;
  
      JSch jsch = new JSch();
  
      public String status(String stringstatus) {
          stringstatus = status;
          return stringstatus;
      }
  
      public String InputCommand(String inputcommandstatus) {
          inputcommandstatus = inputcommand;
          return inputcommandstatus;
      }
  
      public void connect(String usernamelokal, String hostnamelokal,
              String password, int port) {
          //        JSch jsch=new JSch();
          try {
              Session sessionlokal = jsch.getSession(usernamelokal,
                      hostnamelokal, port);
              sessionlokal.setPassword(password);
              UserInfo ui = new UserInfoku.Infoku();
              sessionlokal.setUserInfo(ui);
              sessionlokal.setTimeout(0);
              sessionlokal.connect();
              status = "tersambung \n";
              username = usernamelokal;
              hostname = hostnamelokal;
              session = sessionlokal;
              System.out.println(username + " " + hostname);
          } catch (Exception e) {
              System.out.println(e);
              status = "Exception = \n " + e + "\n";
  
          }
      }
  
      public void disconnect() {
          //        JSch jsch=new JSch();
          try {
              Session sessionlokal = jsch.getSession(username, hostname);
              //            System.out.println(username +" "+ hostname);
              sessionlokal.disconnect();
              status = "wes pedhoott \n";
          } catch (Exception e) {
              System.out.println(e);
              status = "Exception = \n " + e + "\n";
          }
  
      }
  
      public void addRoute() {
          //        JSch jsch=new JSch();
          System.out.println(username + " " + hostname);
          try {
              Session sessionlokal = session; // =jsch.getSession(username, hostname);
              Channel channel = sessionlokal.openChannel("exec");
              ((ChannelExec) channel).setCommand(inputcommand);
              channel.setInputStream(null);
              channel.connect();
              ((ChannelExec) channel).setErrStream(System.err);
              InputStream in = channel.getInputStream();
              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;
                      System.out.print(new String(tmp, 0, i));
                  }
                  if (channel.isClosed()) {
                      System.out.println("exit-status: "
                              + channel.getExitStatus());
                      break;
                  }
                  try {
                      Thread.sleep(1000);
                  } catch (Exception ee) {
                  }
              }
  
              channel.disconnect();
          } catch (Exception e) {
              System.out.println(e);
          }
      }
  }      
  </code></pre>
  
  <p>The problem is when I call the connect method and then calling the addroute, the program returns </p>
  
  <pre><code>root 192.168.50.2
  root 192.168.50.2
  com.jcraft.jsch.JSchException: session is down
  </code></pre>
  
  <p>I've been trying to get session status with either</p>
  
  <pre><code>Session sessionlokal=session; //returns com.jcraft.jsch.JSchException: ChannelExec
  </code></pre>
  
  <p>or </p>
  
  <pre><code>Session sessionlokal=jsch.getSession(username, hostname); //returns session is down
  </code></pre>
  
  <p>I've also tried to use keepalive, but its not working either.</p>
  
  <p>My intention is to create a session to host (log in), while leaving the session up, execute a command or commands and maybe executing other commands later, and then closing the session when its not needed (log out). 
  I've been searching on this forum and I found this <a href="https://stackoverflow.com/questions/7207688/jsch-one-session-multiple-channels">question</a> but the code is create a method to define a command to execute first, and then creating the session, call the command's method and close the session.</p>
  
  <p>Any ideas about how to do as I mentioned above ?</p>