I need to run some commands from a remote computer using an SSH connection, but the problem is the following:
The client computer (running Windows) is connected to a network where I can see a server remote (second *nix computer, in the same network). I can do SSH connections with it, however the computer that contains the files (running *nix) isn't in this network, I only can connect with this trough a dynamic SSH tunnel open in the second computer, where I normally use PuTTY to configure this connection. Then I've got access to the remote files.
The following picture represents the architecture (the firewall is like the second machine):
I need to make this work automatically so I've done some test with Java and the JSch library, here is some code:
/* Direccion y puerto del host local */
String host = "localhost";
int lport = 5040;
/* Direccion y puerto del host remoto*/
String rhost = "localhost";
int rport = 80;
/* Usuario y password para conectarse al servidor ssh*/
String user = "test";
String pwd = "test";
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword("test");
Properties config = new Properties();
config.put("StrictHostKeyChecking","no");
session.setConfig(config);
session.connect();
int assinged_port=session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
I got connection, however when I run a command using object session
, the answer is from the second machine not from the third machine as I expected. I would like to know if there is another library that helps to make this work or I'm using wrong JSch.