动机
我在调查相关错误时偶然发现了这个问题和答案java.io.IOException: The socket is EOF
。因为在我的情况下无法立即更改代码以使用其他 SSH Java 库,并且 @a3.14_Infinity 的说明对我来说不够详细,所以我想添加我的看法。
java.io.IOException:套接字是 EOF - 为什么?
因为这个异常没有太大帮助,所以我首先尝试了Wireshark https://www.wireshark.org/看看线路上发生了什么,但无济于事。所以我配置了sshd_config
(OpenSSH 6.9) 登录DEBUG3
水平并得到答案/var/log/auth.log
我的测试机的文件。它指出了一个致命错误同时尝试与 SSH 客户端(Java SSH 库)协商密钥交换算法。
由于 SSH 服务器和客户端无法就相互密钥交换算法达成一致,OpenSSH 服务器终止与客户端的连接。因此,Java SSH 库代码会抛出异常。
但为什么会发生这种情况呢?
The sshtools.j2ssh
(sshtools:j2ssh-核心:0.2.9 http://search.maven.org/#artifactdetails%7Csshtools%7Cj2ssh-core%7C0.2.9%7Cjar) 库代码非常旧并且已停止使用。从...开始OpenSSH 6.7 http://www.openssh.com/txt/release-6.7(2014 年 10 月发布)默认密码和 MAC 已被更改以删除不安全的算法,其中包括blowfish-cbc
密码。与OpenSSH 6.9 http://www.openssh.com/txt/release-6.9(2015 年 6 月发布)对 1024 位的支持diffie-hellman-group1-sha1
默认情况下禁用密钥交换。
当你还在使用史前的 SSH 工具时j2ssh
库(上帝禁止)连接到较新的 OpenSSH 服务器,您将收到所描述的错误。库代码仅提供diffie-hellman-group1-sha1
OpenSSH 服务器默认不支持的密钥交换算法。因此,无法建立安全连接。
无法更改代码吗?
如果无法立即迁移到另一个 Java SSH 库(我的情况),那么您可以重新启用已禁用的diffie-hellman-group1-sha1
OpenSSH 服务器配置文件中的密钥交换算法sshd_config
。比如像这样。
Ciphers aes128-ctr,aes192-ctr,aes256-ctr,[email protected] /cdn-cgi/l/email-protection,[email protected] /cdn-cgi/l/email-protection,[email protected] /cdn-cgi/l/email-protection,blowfish-cbc
KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,[email protected] /cdn-cgi/l/email-protection,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
但让我澄清一下这一点。这diffie-hellman-group1-sha1
密钥交换算法以及blowfish-cbc
密码默认关闭,因为它们不安全。在您可以替换这个过时的 Java SSH 库之前,重新启用它们应该只是一种临时措施。
Finally, I like to point out that the suggested Java Secure Channel (JSch) library in other answers is discontinued. So, you might want to consider sshj https://github.com/hierynomus/sshj or even ssh2j-maverick https://github.com/sshtools/j2ssh-maverick instead.
编辑:我错了, the Java安全通道JSch http://www.jcraft.com/jsch库已上线(JSCH 0.1.54 于 2016-09-03 发布在 MavenCentral 上 http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.jcraft%22%20AND%20a%3A%22jsch%22)当然值得您考虑。或者,您可能还想考虑sshj https://github.com/hierynomus/sshj or ssh2j-特立独行 https://github.com/sshtools/j2ssh-maverick.
附录:迁移
为了保持迁移努力sshtools.j2ssh
(sshtools:j2ssh-核心:0.2.9 http://search.maven.org/#artifactdetails%7Csshtools%7Cj2ssh-core%7C0.2.9%7Cjar)图书馆最小我看了商业的旧版 SSH 客户端库来自SSHTOOLS https://www.sshtools.com/en/products/java-ssh-client(版本 1.7.1)。这允许保留现有的库集成代码,只需对库 API 和异常处理进行一些小的更改。因此,如果您不想从头开始,那么硬着头皮坚持使用 SSHTOOLS 可能是您最好的选择。最后,为了衡量迁移工作,我首先用 SSHTOOLS 的开源库替换了该库ssh2j-特立独行 https://github.com/sshtools/j2ssh-maverick其 API 与其最新商业版本(版本 1.7.1)几乎相同。