2

我正在尝试使用 SSHJ 将 ssh 连接到其他机器。下面的 PFA 代码(不包括 try/catch/finally 块)。

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
final SSHClient sshClient = new SSHClient();
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
sshClient.connect("test-hostname");
sshClient.authPublickey("test-user", private_key_path);
Session session = sshClient.startSession();
Session.Command cmd = session.exec(TEST_SSH_COMMAND);
cmd.join(5, TimeUnit.SECONDS);
if(cmd.getExitStatus() == 0) {
   System.out.println("Success");
}

当我尝试执行上述程序时,出现以下错误

[reader] n.s.sshj.transport.TransportImpl - Dying because -net.schmizz.sshj.transport.TransportException: Unable to reach a settlement: [] and [aes128-ctr, aes192-ctr, aes256-ctr, arcfour256, arcfour128, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, aes192-cbc, aes256-cbc, arcfour, rijndael-cbc@lysator.liu.se]
2014-07-01 20:45:09,021 INFO  [reader] n.s.sshj.transport.TransportImpl - Disconnected - UNKNOWN
2014-07-01 20:45:09,023 ERROR [pool-3-thread-1] net.schmizz.concurrent.Promise - <<kex done>> woke to: net.schmizz.sshj.transport.TransportException: Unable to reach a settlement: [] and [aes128-ctr, aes192-ctr, aes256-ctr, arcfour256, arcfour128, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, aes192-cbc, aes256-cbc, arcfour, rijndael-cbc@lysator.liu.se]

2014-07-01 20:45:09,024 信息 [pool-3-thread-1] nssshj.transport.TransportImpl - 断开连接 - BY_APPLICATION

有人可以帮我调试这个问题。

谢谢。

4

3 回答 3

1

我有同样的问题,这是一个类加载问题。另一个库 (winzipaes) 依赖于另一个版本 auf Bouncycastle (bcprov-jdk16),这似乎与 SSHJ 引用的 jdk15 版本有冲突。

明确排除 jdk16 版本对我有帮助(但是我还没有测试使用 winzipaes 的代码)。

于 2015-02-24T14:03:58.247 回答
1

我在部署 Cloudera 集群时遇到了同样的问题。确保支持的MAC的客户端和服务器集具有非空交集。

例如我得到:

net.schmizz.sshj.transport.TransportImpl: Dying because - net.schmizz.sshj.transport.TransportException: Unable to reach a settlement: [hmac-sha1, hmac-sha1-96, hmac-md5, hmac-md5-96] and [hmac-sha2-512-etm@openssh.com, hmac-sha2-256-etm@openssh.com, umac-128-etm@openssh.com, hmac-sha2-512, hmac-sha2-256, hmac-ripemd160]

解决方法是将至少一个客户端 MAC 添加到服务器支持的 MAC。在 Ubuntu 14.04.2 LTS 上,SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2只需MACs/etc/ssh/sshd_config.

不太确定这里的安全含义,即某些方法可能会因为弱而被劝阻,但你明白了,客户端和服务器需要解决一个问题。还要注意,是客户端做出选择,服务器会适应。

不要忘记重新启动服务器(如上的 Ubuntu)service ssh restart:.

于 2015-05-19T10:15:58.710 回答
0

我无法找到任何解决此问题的方法。相反,我开始使用 JSch,它现在工作正常。

java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");

Session session = null;
ChannelExec channel = null;

    try {
            JSch jSch = new JSch();
            jSch.addIdentity("/tmp/privatekey");

            session = jSch.getSession("testuser", address, 22);
            session.setConfig(config);
            session.connect();

            channel = (ChannelExec) session.openChannel("exec");
            BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            channel.setCommand(command);
            channel.connect();

            if (channel.getExitStatus() == 0 || channel.isClosed() || channel.isEOF()) {
                logger.info("SSH connection is successful!");                    
            }
            in.close();
        } catch (JSchException jsche) {
            logger.error("Trying to SSH to host: {} but got exception {}", address, jsche);                
        } finally {
            if (channel != null) channel.disconnect();
            if (session != null) session.disconnect();
        }
于 2014-07-16T16:52:00.177 回答