title: SSH tunneling via JSch
body: <p>My aim is to connect to a server (host) which is behind a firewall. I am able to access this server by connecting to another server (tunnel) in the network and then SSH to this server. However I am not able to implement the same scenario via JSch. </p>
  
  <p>I am not able to have the below code work, which I have written for this purpose. Please let me know if I am doing anything silly here.</p>
  
  <pre><code>public class JschExecutor {
  
      public static void main(String[] args){
          JschExecutor t=new JschExecutor();
          try{
              t.go();
          } catch(Exception ex){
              ex.printStackTrace();
          }
      }
      public void go() throws Exception{
  
          StringBuilder outputBuffer = new StringBuilder();
  
          String host="xxx.xxx.xxx.xxx"; // The host to be connected finally
          String user="user";
          String password="passwrd";
          int port=22;
  
          String tunnelRemoteHost="xx.xx.xx.xx"; // The host from where the tunnel is created
  
          JSch jsch=new JSch();
          Session session=jsch.getSession(user, host, port);
          session.setPassword(password);
          localUserInfo lui=new localUserInfo();
          session.setUserInfo(lui);
          session.setConfig("StrictHostKeyChecking", "no");
  
          ProxySOCKS5 proxyTunnel = new ProxySOCKS5(tunnelRemoteHost, 22);
          proxyTunnel.setUserPasswd(user, password);
          session.setProxy(proxyTunnel);
  
          session.connect(30000);
  
          Channel channel=session.openChannel("exec");
          ((ChannelExec)channel).setCommand("hostname");
  
          channel.setInputStream(null);
          ((ChannelExec)channel).setErrStream(System.err);
  
          InputStream in=channel.getInputStream();
          BufferedReader ebr = new BufferedReader(new InputStreamReader(in));
  
          channel.connect();
  
          while (true) {
              byte[] tmpArray=new byte[1024];
              while(in.available()&gt;0){
                  int i=in.read(tmpArray, 0, 1024);
                  if(i&lt;0)break;
                  outputBuffer.append(new String(tmpArray, 0, i)).append("\n");
               }
              if(channel.isClosed()){
                  System.out.println("exit-status: "+channel.getExitStatus());
                  break;
               }
          }
          ebr.close();
  
          channel.disconnect();
  
          session.disconnect();
  
          System.out.println(outputBuffer.toString());
      }
  
    class localUserInfo implements UserInfo{
      String passwd;
      public String getPassword(){ return passwd; }
      public boolean promptYesNo(String str){return true;}
      public String getPassphrase(){ return null; }
      public boolean promptPassphrase(String message){return true; }
      public boolean promptPassword(String message){return true;}
      public void showMessage(String message){}
    }     
  
  } 
  </code></pre>
  
  <p>The above code gives the below exception on the <code>session.connect(30000);</code> line.</p>
  
  <pre><code>com.jcraft.jsch.JSchException: ProxySOCKS5: com.jcraft.jsch.JSchException: fail in SOCKS5 proxy
      at com.jcraft.jsch.ProxySOCKS5.connect(ProxySOCKS5.java:317)
      at com.jcraft.jsch.Session.connect(Session.java:231)
      at com.ukris.main.JschExecutor.go(JschExecutor.java:50)
      at com.ukris.main.JschExecutor.main(JschExecutor.java:19)
  Caused by: com.jcraft.jsch.JSchException: fail in SOCKS5 proxy
      at com.jcraft.jsch.ProxySOCKS5.connect(ProxySOCKS5.java:200)
      ... 3 more
  </code></pre>