title: SSH port fowarding with SSHj
body: <p>I am trying to create a tunnel to use a service behind a firewall, that supports SSH. I wanted a complete solution in java, but I cannot seem to get it to work. I found this <a href="https://github.com/shikhar/sshj/blob/master/src/main/java/examples/RemotePF.java" rel="nofollow" title="github snip">github snip</a> and based on that I created the following code to keep a background thread giving me the tunnel:</p>
  
  <pre><code>// property on surrounding class
  // static final SSHClient sshclient = new SSHClient();
  
  Thread thread = new Thread(new Runnable() {
  
      @Override
      public void run() {
          try {
              String host = "10.0.3.96";
              sshclient.useCompression();
              sshclient.addHostKeyVerifier("30:68:2a:20:21:9f:c8:e8:ac:b4:a7:fc:2d:a7:d0:26");
              sshclient.connect(host);
              sshclient.authPassword("messy", "messy");
              if (!sshclient.isAuthenticated()) {
                  throw new RuntimeException(String.format("Unable to authenticate against '%s'", host));
              }
              Forward forward = new Forward(8111);
              InetSocketAddress addr = new InetSocketAddress("google.com", 80);
              SocketForwardingConnectListener listener = new SocketForwardingConnectListener(addr);
  
              sshclient.getRemotePortForwarder().bind(forward, listener);
              sshclient.getTransport().setHeartbeatInterval(30);
  
              HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8111").openConnection();
              con.setRequestMethod("GET");
              BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
              String line = null;
              while( (line = reader.readLine()) != null) {
                  System.out.println(line);
              }
  
              sshclient.getTransport().join();
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              if (sshclient != null &amp;&amp; sshclient.isConnected()) {
                  sshclient.disconnect();
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  });
  thread.setDaemon(true);
  thread.start();
  </code></pre>
  
  <p>The problem is that if I connect to a remote SSH server like '10.0.3.96' it does not work. If I use the local SSH server on my localhost it will work. I have been going thru different configurations with any luck and tried debugging, but I cannot grasp what is going on inside the SSHj package.</p>
  
  <p>Now it does not have to be a SSHj solution, but that would be preferred since other parts of the code are fully implemented and using SSHj and I do not want to mix two SSH packages in one project.</p>
  
  <p>Thanks for any help.</p>