title: Not able to connect to SFTP using jcraft JSch
body: <p>I am trying to connect to SFTP location via ssh jump-host proxy using jcraft version 0.1.52. But getting "<em>connection is closed by foreign host</em>" exception in my code. I have spent a good enough time looking at documentation but not able to figure out what the problem is</p>
  
  <pre><code>2016-11-18 14:53:14,091 44977 [main] ERROR c.w.v.r.ftp.JschSftpConnect -
      - com.jcraft.jsch.JSchException: connection is closed by foreign host
          at com.jcraft.jsch.Session.connect(Session.java:269)
          at com.jcraft.jsch.Session.connect(Session.java:183)
          at com.x.y.ftp.JschSftpConnect.connect(JschSftpConnect.java:77)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  </code></pre>
  
  <p>There is no problem with private keys as I am able to connect to sftp server via unix command.</p>
  
  <pre><code> sftp -o UserKnownHostsFile=/dev/null 
    -o StrictHostKeyChecking=no 
    -i /path/to/host/private-key-file 
      -o 'ProxyCommand=ssh 
          -o UserKnownHostsFile=/dev/null 
          -o StrictHostKeyChecking=no 
          -i /path/to/jumphost/private-key-file
          -l jumphostuser jump.host.com nc sftp.host.com 22' 
  </code></pre>
  
  <ol start="3">
  <li>Here is the code that I am running</li>
  </ol>
  
  <p>The code:</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.Socket;
  import java.nio.file.FileSystems;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.util.Properties;
  
  import org.apache.commons.lang3.exception.ExceptionUtils;
  
  import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelSftp;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Proxy;
  import com.jcraft.jsch.Session;
  import com.jcraft.jsch.SocketFactory;
  
  public class JschSftpConnect {
  
  
      public static void main(String args[]) {
          String sftpHostKeyFilePath = "/path/to/host/private-key-file";
          String sftpHost="sftp.host.com";
          String sftpUser="user";
          String proxyHostName="jump.host.com";
          String proxyKeyPath ="/path/to/jumphost/private-key-file";
          String proxyUserName="jumphostuser";
  
          log.debug("Executing JSCH code.....");
          try {
  
              JSch jsch = new JSch();
  
              Path path = FileSystems.getDefault().getPath(sftpHostKeyFilePath);
              byte[] filearray = Files.readAllBytes(path);
  
              jsch.addIdentity("ID", filearray, null, null);
              Session session = jsch.getSession(sftpUser, sftpHost, 22);
              Properties props = new Properties();
              props.put("StrictHostKeyChecking", "no");
              session.setConfig(props);
  
              String command = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "
                  + localKeyFileDirectoryName + proxyKey + " -l " + proxyUserName + " " + proxyHostName
                  + " nc %h %p";
  
              session.setProxy(new JumpHostProxyCommand(command));
              log.debug("Connecting session .......................");
              session.connect();
              log.debug("Session openend .......................");
  
              Channel ch = session.openChannel("sftp");
              ch.connect();
              log.debug("SFTP channel connected .......................");
              ChannelSftp channelSftp = (ChannelSftp) ch;
              channelSftp.cd("/");
  
              log.debug("Working directory is / .......................");
  
              byte[] buffer = new byte[1024];
              BufferedInputStream bis = new BufferedInputStream(channelSftp.get("/some_file_.psv"));
              File newFile = new File("some_file_.psv");
              OutputStream os = new FileOutputStream(newFile);
              BufferedOutputStream bos = new BufferedOutputStream(os);
              int readCount;
              // System.out.println("Getting: " + theLine);
              while ((readCount = bis.read(buffer)) &gt; 0) {
                  // System.out.println("Writing: ");
                  bos.write(buffer, 0, readCount);
              }
              log.debug("File should have been written / .......................");
  
              while (session != null) {
                  System.out.println("Killing the session");
                  session.disconnect();
                  bis.close();
                  bos.close();
                  System.exit(0);
              }
          } catch (Exception e) {
              log.error(ExceptionUtils.getStackTrace(e));
          }
      }
  }
  
  
  
  class JumpHostProxyCommand implements Proxy {
  
      String command;
      Process p = null;
      InputStream in = null;
      OutputStream out = null;
  
      public JumpHostProxyCommand(String command) {
          this.command = command;
      }
  
      public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws Exception {
  
  
          String cmd = command.replace("%h", host);
          cmd = cmd.replace("%p", new Integer(port).toString());
  
          p = Runtime.getRuntime().exec(cmd);
          log.debug("Process returned by proxy command {} , {}", command,  p);
          in = p.getInputStream();
          log.debug("Input stream returned by proxy {}", in);
          out = p.getOutputStream();
          log.debug("Output stream returned by proxy {}", out);
      }
  
      public Socket getSocket() {
          return null;
      }
  
      public InputStream getInputStream() {
          return in;
      }
  
      public OutputStream getOutputStream() {
          return out;
      }
  
      public void close() {
          try {
              if (p != null) {
                  p.getErrorStream().close();
                  p.getOutputStream().close();
                  p.getInputStream().close();
                  p.destroy();
                  p = null;
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Would really appreciate if someone could help. Thanks!</p>