5

I want to open up a SSH tunnel in a Java program, so that the rest of the system will also have access to the connection (e.g. if I forward a port for a database to the localhost I should be able to open the database in another program)... In other words what I need to do it to create a Java wrapper for something like the unix command ssh. However I need the program to be cross platform, so if possible I would like to use a library from Java.

Is this possible (I have been looking at different libraries, but have not been able to create the tunnel so the rest of the system can use it yet)

2
  • @Bobby sometimes using such a solution is good. Commented Mar 17, 2011 at 8:51
  • @Bobby SSH tunnels are the swiss army knife of heavily firewalled environments. As long as everything is meant to be temporary they are a lot easier to use than setting up full blown VPNs. Commented Mar 17, 2011 at 8:55

4 Answers 4

5

The JSch library definitely makes this easy and supports port forwarding:

JSch jsch = new JSch();
Session session = jsch.getSession(user, host);
session.setPassword(password);
session.connect(timeout);
session.setPortForwardingL(listenPort, destHost, destPort);
Sign up to request clarification or add additional context in comments.

1 Comment

Notes:it is only port forwarding,but not a dynamic sock forwarding which plink-D did.otherwise,you will get I/O exception:(java.net.SocketException) caught when connecting to the target host: Reply from SOCKS server has bad version.
5

I had a success with article from beanizer based on Jcraft's Jsch. Here are more details: http://www.beanizer.org/site/index.php/en/Articles/Java-ssh-tunneling-with-jsch.html

Comments

0

Perhaps Jsch ?

2 Comments

is this supposed to be an answer?
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
0

I have been using Ganymede SSH-2 for exactly this purpose with great success, worked first time actually, much to my surprise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.