title: Automation of ssh session using jsch library in java
body: <p>I am automating the ssh session using java, using this code i am able to login and i get the output on screen, but after this, after we enter the application to run in the ssh session, it doesn't take username and password as it requires keyboard interactions like TAB and ENTER and function keys etc, so is there a way to send keyboard events in place of commands so that automation is successful?</p>
  
  <pre><code>public class jschputty {
  
  public static void main(String args[])
  {   
   String host="x.x.x.x";
   String user="username";
   String password="password";
   String command1="ls -ltr\t";
  
   try{
  
      java.util.Properties config = new java.util.Properties(); 
      config.put("StrictHostKeyChecking", "no");
      JSch jsch = new JSch();
      Session session=jsch.getSession(user, host, 22);
      session.setPassword(password);
      session.setConfig(config);
      session.connect();
      System.out.println("Connected");
  
      ChannelExec channel=(ChannelExec) session.openChannel("exec");
      BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
  
    channel.setCommand(command);
      channel.connect();
             String msg=null;
             while((msg=in.readLine())!=null){
              System.out.println(msg);
             }                  
                  channel.disconnect();
                  session.disconnect();
                  System.out.println("DONE");
              }catch(Exception e){
                  e.printStackTrace();
              }
  
          }
  </code></pre>
  
  <p>}</p>