Transfer file trough SSH tunnel with Java

2019-06-01 17:13发布

I need to get some files from a remote computer using an SSH/SFTPconnection, but the problem is the following:

The client computer (running Windows), where I'll run my application, is connected to a network where I can see a server remote (second computer, running Unix, in the same network). I can do SSH connections with it, however the computer that contains the files (running Unix) isn't in this network, I only can connect with this trough a dynamic tunnel SSH open in the second computer, where I normally use PuTTY for 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)

Architecture

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.

2条回答
ら.Afraid
2楼-- · 2019-06-01 18:02

It's the same answer as to your other question (where you are using a shell connection instead of file transfer):

Put the right hostName as rhost, namely the name of the target server as seen from the firewall server.

If it works with HTTP, I suppose that the firewall server also has a HTTP proxy running which forwards requests on port 80 to your target port ... and of course, it can't do this for SSH if it also should be an SSH server itself.

If you want to do both connections from JSch, instead of doing a local port forwarding and then connecting to this forwarded port, you can use my ProxySSH class, to find in the JSch wiki.

查看更多
祖国的老花朵
3楼-- · 2019-06-01 18:04

Use a local port forwarding, aka an SSH tunnel, to open an SSH/SFTP connection to server via firewall. Then you can directly download the file to server from your local machine:

Session firewallSession = jsch.getSession("firewall_username", "firewall", 22);
// ...
firewallSession.connect();

int forwardedPort = 2222; // any port number which is not in use on the local machine
firewallSession.setPortForwardingL(forwardedPort, "server", 22);

Session session = jsch.getSession("server_usernam", "localhost", forwardedPort);
// ...
session.connect();

Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp)channel;           

channelSftp.get("/remote/path/on/server/file.txt", "C:\\local\\path\\file.txt");
查看更多
登录 后发表回答