如何使用 JSch SSH 到另一个 SSH 服务器后面的服务器?

发布于 2024-11-25 11:35:12 字数 528 浏览 1 评论 0原文

我需要能够从 Java 程序 ssh 到远程服务器,并从那里 SSH 到另一台服务器。我的客户端上有两台服务器的凭据。

命令将作为常规字符串从应用程序内自动传递(无需用户输入)。我需要能够在第二台服务器上运行这些自定义命令,并能够根据输出和一些简单的逻辑来决定在运行时发出哪些命令。

我可以使用 JSch 来做到这一点吗?如果可以,我应该从哪里开始研究? (示例,信息)

=============================================== ================

添加:

线程“主”com.jcraft.jsch.JSchException 中出现异常: 未知主机密钥:host.net。 RSA 密钥指纹是“blahblahblah”

到目前为止, ,我通过修改known_hosts 文件并在其中手动添加主机来解决这个问题。 我可以通过在某处设置一个选项来告诉 JSch 在询问此 YES-NO 问题时自动按 YES 来绕过这个小问题吗?

I need to be able to ssh from a Java program into a remote server, and from there SSH to another server. I have credentials for both servers on my client.

The commands will be passed automatically from within the app as regular strings (no user input). I need to be able to run those custom commands on the second server and be able to decide what commands to issue during runtime, based on the output and some simple logic.

Can I use JSch to do that and if yes, where should I start look into? (Examples, info)

=============================================================

ADDED:

Exception in thread "main" com.jcraft.jsch.JSchException:
UnknownHostKey: host.net. RSA key fingerprint is 'blahblahblah'

as till now, I am solving this problem by modifying the known_hosts file and adding host manually in there.
Can I bypass this little problem by settings an option somewhere telling the JSch to press YES automatically when this YES-NO question is asked?

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

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

发布评论

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

评论(3

几味少女 2024-12-02 11:35:12

要连接到防火墙后面的第二个服务器,原则上有两种选择。

天真的做法是在第一台服务器上调用 ssh(从 exec 通道),指示正确的服务器。这需要使用 JSch 进行代理转发,并且不提供 JSch API 来访问第二个服务器,仅提供 ssh 命令行。

更好的方法是使用与第一台服务器的连接建立 TCP 隧道,并使用该隧道连接到第二台服务器。 JSch Wiki 包含一个 ProxySSH 类(以及一些示例代码) ),它允许使用 JSch 会话作为第二个 JSch 会话的隧道。 (免责声明:这个类主要是由我编写的,并得到了 JSch 作者的一些支持。)

当您连接到第二个服务器时,请使用 shell 通道或一系列 exec 执行命令的通道。 (请参阅 JSch 中的 Shell、Exec 或子系统通道 Wiki 提供概述,Javadocs 提供详细信息。)


对于您的未知主机密钥问题:

安全版本是收集之前的所有主机密钥(以安全的方式)并将它们放入known_hosts文件中。 (如果您只是信任提供给您的密钥,则很容易受到中间人攻击。如果您的网络中不关心这些,因为它是物理安全的,那么对您有好处。

) em>方便版本正在设置 配置选项 StrictHostKeyCheckingno - 这会将未知的主机密钥添加到主机密钥文件中:(

JSch.setConfig("StrictHostKeyChecking", "no");

如果您只想在会话上单独设置它,您也可以为代理会话而不是隧道会话设置它,或者使用 yesask 覆盖隧道会话 - 这样 MITM 的危险可能会更大。

中间道路是启用实际询问用户(然后应该将指纹与某个列表进行比较) - 为此,请实现 UserInfo 接口并向会话提供对象。 (JSch Wiki 包含一个使用 Swing JOptionPanes 的示例实现,其中如果您的客户端程序在具有 GUI 的系统上运行,则可以简单地使用。)

为了保存接受的主机密钥,您必须使用 JSch.setKnownHosts< /a> 带有文件名参数的方法,而不是带有 InputStream 参数的方法 - 否则每次重新启动客户端时都必须重复接受。

To connect to a second server behind a firewall, there are in principle two options.

The naive one would be to call ssh on the first server (from an exec channel), indicating the right server. This would need agent forwarding with JSch, and also doesn't provide the JSch API to access the second server, only the ssh command line.

The better one would be to use the connection to the first server to build up a TCP Tunnel, and use this tunnel to connect to the second server. The JSch Wiki contains a ProxySSH class (together with some example code) which allows to use a JSch session as a tunnel for a second JSch session. (Disclaimer: This class was written mainly by me, with some support from the JSch author.)

When you have your connection to the second server, use either a shell channel or a series of exec channels to execute your commands. (See Shell, Exec or Subsystem Channel in the JSch Wiki for an overview, and the Javadocs for details.)


For your unknown-host-key problem:

The secure version would be to collect all host keys (in a secure way) before and put them in the known_hosts file. (If you simply trust the key which is presented to you, you are vulnerable to a man-in-the-middle attack. If these are of no concern in your network, since it is physically secured, good for you.)

The convenient version is setting the configuration option StrictHostKeyChecking to no - this will add unknown host keys to the host keys file:

JSch.setConfig("StrictHostKeyChecking", "no");

(You can also set it individually on the sessions, if you only want to set it for the proxied sessions and not for the tunnel session. Or override it for the tunnel session with yesor ask - there the MITM danger might be greater.)

A middle way would be to enable actually asking the user (which then should compare the fingerprints to some list) - for this, implement the UserInfo interface and provide the object to the session. (The JSch Wiki contains an example implementation using Swing JOptionPanes, which you can simply use if your client program runs on a system with GUI.)

For the saving of accepted host keys to work, you must use the JSch.setKnownHosts method with a file name argument, not the one with an InputStream argument - else your accepting will have to be repeated for each restart of your client.

汹涌人海 2024-12-02 11:35:12

使用 SSH 隧道,又名 本地端口转发,通过以下方式打开到 B 的 SSH/SFTP 连接答:

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

int forwardedPort = sessionA.setPortForwardingL(0, "hostB", 22);

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

// Use sessionB here for shell/exec/sftp

您可能需要处理 UnknownHostKey 异常 >

Use an SSH tunnel, aka local port forwarding, to open an SSH/SFTP connection to B via A.

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

int forwardedPort = sessionA.setPortForwardingL(0, "hostB", 22);

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

// Use sessionB here for shell/exec/sftp

You may need to deal with UnknownHostKey exception.

回心转意 2024-12-02 11:35:12

这可以帮助任何人。工作正常:

 public static void sesionA(){
     try {
        Session sessionA = jSch.getSession(username, hostA);  
        Properties config = new Properties(); 
        config.put("StrictHostKeyChecking", "no");
        sessionA.setConfig(config);
        sessionA.setPassword(passwordA);
        sessionA.connect();


        if(sessionA.isConnected()) {
            System.out.println("Connected host A!");
            forwardedPort = 2222;
            sessionA.setPortForwardingL(forwardedPort, hostB, 22);      
        }

    } catch (JSchException e) {
        e.printStackTrace();
    }
 }

 public static void sesionB(){


        try {
            Session sessionB = jSch.getSession(username, "localhost", forwardedPort);

            Properties config = new Properties(); 
            config.put("StrictHostKeyChecking", "no");
            sessionB.setConfig(config);
            sessionB.setPassword(passwordB);
            sessionB.connect();

          if(sessionB.isConnected()) {
             System.out.println("Connected host B!");
          }
     }
 }

This can help anyone. Works fine:

 public static void sesionA(){
     try {
        Session sessionA = jSch.getSession(username, hostA);  
        Properties config = new Properties(); 
        config.put("StrictHostKeyChecking", "no");
        sessionA.setConfig(config);
        sessionA.setPassword(passwordA);
        sessionA.connect();


        if(sessionA.isConnected()) {
            System.out.println("Connected host A!");
            forwardedPort = 2222;
            sessionA.setPortForwardingL(forwardedPort, hostB, 22);      
        }

    } catch (JSchException e) {
        e.printStackTrace();
    }
 }

 public static void sesionB(){


        try {
            Session sessionB = jSch.getSession(username, "localhost", forwardedPort);

            Properties config = new Properties(); 
            config.put("StrictHostKeyChecking", "no");
            sessionB.setConfig(config);
            sessionB.setPassword(passwordB);
            sessionB.connect();

          if(sessionB.isConnected()) {
             System.out.println("Connected host B!");
          }
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文