Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple SSH Tunnel in Java [closed]

Tags:

java

ssh

tunnel

I want to create an SSH Tunnel in Java. I noticed quite a few Java SSH libraries on another post. Before I dig into each option, maybe someone can give me some code snippets of how they did it or at least tell me which client library would work best.

I only need tunneling. I won't need stuff like file transfers, terminal emulation, etc. Is there a simple few lines of code that can forward a port on the server to work on my client's localhost adapter? Ideally both client and server would be in Java, but I'll settle for just client for now.

like image 697
User1 Avatar asked Nov 04 '09 22:11

User1


2 Answers

Well, as pointed out in the other question, JSch is indeed a great choice and has several examples here. The PortForwardingL.java class might be a good starting point.

like image 62
Pascal Thivent Avatar answered Sep 17 '22 04:09

Pascal Thivent


You can do this with several libraries. My favorite is the ssh library inside MindTerm package,

http://linuxmafia.com/pub/java/ISNetworks-MindTerm-1.2.1-SCP3.tar.gz

You can open a tunneled connection like this,

  SSHSocketFactory fact = new SSHSocketFactory(sshHost, sshPort, new SSHPasswordAuthenticator(sshUser, sshPassword));

  sock = fact.createSocket(host, port);
like image 24
ZZ Coder Avatar answered Sep 20 '22 04:09

ZZ Coder