用于Java的SSH库

rpe*_*rez 185 java ssh ssh-tunnel

有没有人知道从Java登录SSH的好库.

Dav*_*itz 118

Java的安全通道(JSCH)是一种非常流行的库,Maven的,蚂蚁和Eclipse使用.它是开源的,具有BSD风格的许可证.

  • 我前段时间尝试过使用JSch,无法理解它是如何受欢迎的.它绝对没有提供任何文档(甚至不在源代码内)和可怕的API设计(http://techtavern.wordpress.com/2008/09/30/about-jsch-open-source-project/总结得很好) (69认同)
  • 是的Jsch很糟糕,这是更好的:https://github.com/shikhar/sshj (13认同)
  • 使用javadoc的公共方法的JSch变体:https://github.com/ePaul/jsch-documentation (3认同)
  • http://stackoverflow.com/questions/2405885/any-good-jsch-examples/11902536#11902536包含使用JSCH运行命令并获取输出的示例. (3认同)
  • 您需要从https://sourceforge.net/projects/jsch/files/jsch/jsch-0.1.42.zip/download下载源代码并运行"ant javadoc" (2认同)

mik*_*iku 61

更新:GSOC项目及其中的代码未激活,但这是:https: //github.com/hierynomus/sshj

自2015年初以来,hierynomus接管了维护人员.这是旧的,不再维护的,Github链接:

https://github.com/shikhar/sshj


有一个GSOC项目:

http://code.google.com/p/commons-net-ssh/

代码质量似乎比JSch更好,JSch虽然完整且有效,但缺乏文档.项目页面显示即将发布的测试版,最后一次提交到存储库的时间是8月中旬.

比较API:

http://code.google.com/p/commons-net-ssh/

    SSHClient ssh = new SSHClient();
    //ssh.useCompression(); 
    ssh.loadKnownHosts();
    ssh.connect("localhost");
    try {
        ssh.authPublickey(System.getProperty("user.name"));
        new SCPDownloadClient(ssh).copy("ten", "/tmp");
    } finally {
        ssh.disconnect();
    }
Run Code Online (Sandbox Code Playgroud)

http://www.jcraft.com/jsch/

Session session = null;
Channel channel = null;

try {

JSch jsch = new JSch();
session = jsch.getSession(username, host, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
session.connect();

// exec 'scp -f rfile' remotely
String command = "scp -f " + remoteFilename;
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);

// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();

channel.connect();

byte[] buf = new byte[1024];

// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();

while (true) {
    int c = checkAck(in);
    if (c != 'C') {
        break;
    }

    // read '0644 '
    in.read(buf, 0, 5);

    long filesize = 0L;
    while (true) {
        if (in.read(buf, 0, 1) < 0) {
            // error
            break;
        }
        if (buf[0] == ' ') {
            break;
        }
        filesize = filesize * 10L + (long) (buf[0] - '0');
    }

    String file = null;
    for (int i = 0;; i++) {
        in.read(buf, i, 1);
        if (buf[i] == (byte) 0x0a) {
            file = new String(buf, 0, i);
            break;
        }
    }

    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();

    // read a content of lfile
    FileOutputStream fos = null;

    fos = new FileOutputStream(localFilename);
    int foo;
    while (true) {
        if (buf.length < filesize) {
            foo = buf.length;
        } else {
            foo = (int) filesize;
        }
        foo = in.read(buf, 0, foo);
        if (foo < 0) {
            // error
            break;
        }
        fos.write(buf, 0, foo);
        filesize -= foo;
        if (filesize == 0L) {
            break;
        }
    }
    fos.close();
    fos = null;

    if (checkAck(in) != 0) {
        System.exit(0);
    }

    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();

    channel.disconnect();
    session.disconnect();
}

} catch (JSchException jsche) {
    System.err.println(jsche.getLocalizedMessage());
} catch (IOException ioe) {
    System.err.println(ioe.getLocalizedMessage());
} finally {
    channel.disconnect();
    session.disconnect();
}

}
Run Code Online (Sandbox Code Playgroud)

  • 我应该提到我是GSOC的学生:) (4认同)
  • 谢谢!我使用Apache SSHD代码(提供异步API)作为种子,为项目提供了一个kickstart. (2认同)
  • 很高兴地宣布http://github.com/shikhar/sshj - 你可以在那里找到jar,找出如何在maven repo上获取它 (2认同)

Ed *_*nin 23

我刚刚发现了sshj,它似乎比JSCH有更简洁的API(但它需要Java 6).这篇文档主要是通过回购中的示例,通常这对我来说已经足够了,但是对于我刚刚开始的项目来说,它似乎已经足够好了.

  • SSHJ实际上可以由来自外部世界的人工作.JSCH是一堆糟糕的文档和API设计,隐藏着很多难以理解的依赖关系.除非您想花费大量时间浏览代码以尝试弄清楚是什么,否则请使用SSHJ.(我希望我对JSCH感到苛刻或狡猾.我真的这么做.) (3认同)
  • 是的,sshj。我尝试过的一切都有效:SCP、远程进程执行、本地和远程端口转发、使用 [jsch-agent-proxy](http://www.jcraft.com/jsch-agent-proxy) 的代理代理。JSCH 一团糟。 (2认同)

msh*_*rir 18

看看最近发布的SSHD,它基于Apache MINA项目.

  • 然而,使用它,缺乏文档和示例. (2认同)

Sco*_*ott 5

在github上有一个全新版本的Jsch:https://github.com/vngx/vngx-jsch一些改进包括:全面的javadoc,增强的性能,改进的异常处理和更好的RFC规范遵从性.如果您希望以任何方式提供帮助,请打开问题或发送拉取请求.

  • 太糟糕了,现在已经有三年没有新的承诺了. (4认同)