SSH 连接 Java

发布于 2024-09-11 04:48:35 字数 29 浏览 3 评论 0原文

是否可以使用java与服务器建立ssh连接?

Is it possible to make a ssh connection to a server with java?

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

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

发布评论

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

评论(4

不羁少年 2024-09-18 04:48:36

是的,我在 Java 应用程序中使用了 http://sourceforge.net/projects/sshtools/通过 SSH 连接到 UNIX 服务器,效果很好。

Yes, I used http://sourceforge.net/projects/sshtools/ in a Java application to connect to a UNIX server over SSH, it worked quite well.

一抹微笑 2024-09-18 04:48:36

jschsshJ 都是很好的客户端。我个人会使用 sshJ,因为代码的文档记录得更彻底。

jsch 有着广泛的使用,包括在 eclipse 和 apache ant 中。我也遇到了 jsch 和 AES 加密私钥的问题,需要在 3DES 中重新加密,但这可能只是我的问题。

jsch and sshJ are both good clients. I'd personally use sshJ as the code is documented much more thoroughly.

jsch has widespread use, including in eclipse and apache ant. I've also had issues with jsch and AES encrypted private keys, which required re-encrypting in 3DES, but that could just be me.

爺獨霸怡葒院 2024-09-18 04:48:36

是的,这是可能的。您可以尝试以下代码:

package mypackage;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;

public class SSHReadFile
    {
    public static void main(String args[])
    {
    String user = "user";
    String password = "password";
    String host = "yourhostname";
    int port=22;

    String remoteFile="/home/john/test.txt";

    try
        {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
            System.out.println("Connection established.");
        System.out.println("Crating SFTP Channel.");
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
        System.out.println("SFTP Channel created.");
        }
    catch(Exception e){System.err.print(e);}
    }
    }

Yes, it is possible. You can try the following code:

package mypackage;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;

public class SSHReadFile
    {
    public static void main(String args[])
    {
    String user = "user";
    String password = "password";
    String host = "yourhostname";
    int port=22;

    String remoteFile="/home/john/test.txt";

    try
        {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
            System.out.println("Connection established.");
        System.out.println("Crating SFTP Channel.");
        ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
        sftpChannel.connect();
        System.out.println("SFTP Channel created.");
        }
    catch(Exception e){System.err.print(e);}
    }
    }
沫雨熙 2024-09-18 04:48:36

要连接到 Java 服务器,您需要 SSHD 的实现(ssh 客户端还不够)。您可以尝试 Apache SSHD,

http://mina.apache.org/sshd/

因为 sshd 是已经在大多数系统上运行,一个更简单的替代方案是通过 SSH 隧道连接到服务器。

To make connection to Java servers, you need an implementation of SSHD (ssh client is not enough). You can try Apache SSHD,

http://mina.apache.org/sshd/

Because sshd is already running on most systems, an easier alternative is to connect to the server through a SSH tunnel.

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