在Java中通过SSH隧道运行远程命令

发布于 2025-01-06 03:36:05 字数 1154 浏览 1 评论 0原文

我需要使用 SSH 连接从远程计算机运行一些命令,但问题如下:

客户端计算机(运行 Windows)连接到网络,我可以在其中看到远程服务器(第二台 *nix 计算机,在同一个网络中)网络)。我可以用它进行 SSH 连接,但是包含文件(运行 *nix)的计算机不在该网络中,我只能通过第二台计算机中打开的动态 SSH 隧道与其连接,我通常在其中使用 PuTTY配置此连接。然后我就可以访问远程文件了。

下图代表了架构(防火墙就像第二台机器):

Architecture

我需要自动完成这项工作,所以我已经使用 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):

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶底世界 2025-01-13 03:36:05

当然,如果您设置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.)

梦过后 2025-01-13 03:36:05

端口转发后,您必须在第二台服务器上创建会话。然后建立与防火墙后面的服务器的连接。

查看示例的代码。这段代码的作用是创建一个本地端口转发到目标系统上的 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.

平定天下 2025-01-13 03:36:05

您必须打开另一个使用转发端口的会话。

Session sessionA = jsch.getSession("usernameA", "hostA");
// ...
sessionA.connect();

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

Session sessionB = jsch.getSession("usernameB", "localhost", forwardedPort);
// ...
sessionB.connect();

// Use sessionB here to execute your command

You have to open another session that makes use of the forwarded port.

Session sessionA = jsch.getSession("usernameA", "hostA");
// ...
sessionA.connect();

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

Session sessionB = jsch.getSession("usernameB", "localhost", forwardedPort);
// ...
sessionB.connect();

// Use sessionB here to execute your command
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文