在Java中通过SSH隧道运行远程命令
我需要使用 SSH 连接从远程计算机运行一些命令,但问题如下:
客户端计算机(运行 Windows)连接到网络,我可以在其中看到远程服务器(第二台 *nix 计算机,在同一个网络中)网络)。我可以用它进行 SSH 连接,但是包含文件(运行 *nix)的计算机不在该网络中,我只能通过第二台计算机中打开的动态 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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,如果您设置rhost =“localhost”,连接将到达SSH服务器,而不是“资源”服务器。
输入正确的主机名,应该没问题。 (我做过一次。)
如果您想从 JSch 进行两个连接,而不是进行本地端口转发然后连接到此转发的端口,您可以使用我的 ProxySSH 类来查找 在 JSch wiki 中。 (我也这样做过,在与您类似的情况下。)
Of course, if you set
rhost = "localhost"
, the connection will arrive at the SSH server, and not at the "Resource" server.Put the right host name in, and it should be no problem. (I did this once.)
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. (I did this, too, in a similar situation like you have there.)端口转发后,您必须在第二台服务器上创建会话。然后建立与防火墙后面的服务器的连接。
查看示例的代码。这段代码的作用是创建一个本地端口转发到目标系统上的 ssh 端口,然后通过它进行连接。 hostname 命令的运行说明它确实在转发到的系统上运行。
You have to create session on the second server after port forwarding. Then connection is established to the server behind firewall.
Look at this code for example.What this code does is create a local port forwarding to the ssh port on the target system, then connects through it. The running of the hostname command illustrates that it is, indeed, running on the forwarded-to system.
您必须打开另一个使用转发端口的会话。
You have to open another session that makes use of the forwarded port.