使用 Java 通过 SSH 隧道传输文件
我需要使用 SSH/SFTP 连接从远程计算机获取一些文件,但问题如下:
我将在其中运行应用程序的客户端计算机(运行 Windows)已连接到可以看到服务器的网络远程(第二台计算机,运行 Unix,位于同一网络中)。我可以与它进行 SSH 连接,但是包含文件(运行 Unix)的计算机不在该网络中,我只能通过在第二台计算机上打开的动态隧道 SSH 与其连接,我通常在其中使用 PuTTY 进行配置通过此连接,我就可以访问远程文件了。
下图代表了架构,(防火墙就像第二台机器)
我需要使其自动工作,所以我已经使用 Java 和 JSch 库进行了一些测试,这里是一些代码:
/* 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);
我已连接,但是当我使用对象 session
运行命令时,答案来自第二台机器,而不是来自第三台机器正如我所料,我想知道,如果还有另一个库可以帮助完成这项工作,或者我使用了错误的 JSch。
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)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与您的其他问题的答案相同(您使用的是 shell 连接而不是文件传输):
将右侧hostName 为
rhost
,即从防火墙服务器看到的目标服务器的名称。如果它与 HTTP 一起工作,我想防火墙服务器也有一个正在运行的 HTTP 代理,它将端口 80 上的请求转发到你的目标端口......当然,如果它也应该是 SSH,它就不能为 SSH 执行此操作服务器本身。
如果您想从 JSch 进行这两个连接,而不是进行本地端口转发然后连接到此转发的端口,您可以使用我的 ProxySSH 类来查找 在 JSch wiki 中。
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.使用本地端口转发,也称为SSH 隧道,通过以下方式打开与服务器的 SSH/SFTP 连接防火墙。然后你可以直接从本地机器下载文件到服务器:
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: