title: How to do a ssh from one machine to another machine using Java
body: <p>Using putty i am able to do an ssh from one machine to another machine and from there another.Like a chain process.I want to replicate the same in my programme.So far i am able to ssh to the 1st machine.From that machine how do i ssh to the subsequent machines.Also i want to execute some commands like 'pwd' or 'ls' .</p>
  
  <pre><code>import com.jcraft.jsch.ChannelSftp;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  import java.io.*;
  
  public class SSH {
  
     public static void main(String args[]) {
  
       String user = "******";
       String password = "******";
       String host = "******";
       int port=22;
  
       String remoteFile="something.txt";
  
       try {
          JSch jsch = new JSch();
          Session session = jsch.getSession(user, host, port);
          session.setPassword(password);
          java.util.Properties config = new java.util.Properties(); 
          config.put("StrictHostKeyChecking", "no");
          session.setConfig(config);
          session.connect();
          ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
          sftpChannel.connect();
          System.out.println("connected to first server....");
          InputStream out= null;
          out= sftpChannel.get(remoteFile);
          BufferedReader br = new BufferedReader(new InputStreamReader(out));
          String line;
          while ((line = br.readLine()) != null)
             System.out.println(line);
          br.close();
  
          /********* Here i want to ssh to another machine from the already connected one ******/
  
      String command = "pwd" ; //executing correctly with o/p
      Channel channel = session.openChannel("exec");
      ((ChannelExec) channel).setCommand(command);
      channel.setInputStream(null);
      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;
        }
  
      }
      channel.disconnect();
  
      Channel channel1 = session.openChannel("exec");
      ((ChannelExec) channel1).setCommand("ssh username@hostname" + "&amp;&amp;" + "password");//not    executing
      channel1.setInputStream(null);
      in = channel1.getInputStream();
      channel1.connect();
      channel1.disconnect();
  
  
  
      Channel channel2 = session.openChannel("exec");
  
      ((ChannelExec) channel2).setCommand("ls"); //to verify if ssh to 2nd machine has happened.not working
       channel2.setInputStream(null);
       in = channel2.getInputStream();
      channel2.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: "+channel2.getExitStatus());
          break;
        }
  
      }
      channel2.disconnect();
      session.disconnect();
       } catch(Exception e) { System.err.print(e); }
     }
  
  }
  </code></pre>