使用ganymed-ssh2连接linux报错 Cannot negotiate, proposals do not match

ganymed-ssh2连接linux报错

背景

最近有个项目,需要在Linux下的服务器内写了一部分Python脚本,业务处理却是在Java内,那么就想能不能在Java里面,来远程执行Linux下的Python脚本,就是把shell命令写到Java里面。于是上网查了下,还真有第三方jar库,ganymed-ssh2。专门给java提供远程执行ssh的一个第三方jar包,于是一顿操作接入后,一执行,发现报错!!相信很多同学第一次接入也会报这个错误,看看是不是和大家的一样。

报错信息

Caused by: java.io.IOException: There was a problem while talking to 127.0.0.1:22
at ch.ethz.ssh2.Connection.connect(Connection.java:642)
at com.ultrapower.driver.resdriver.transport.SSHProtocol.ini(SSHProtocol.java:94)2 more
Caused by: java.io.IOException: Key exchange was not finished, connection is closed.
at ch.ethz.ssh2.transport.KexManager.getOrWaitForConnectionInfo(KexManager.java:90)
at ch.ethz.ssh2.transport.TransportManager.getConnectionInfo(TransportManager.java:221)
at ch.ethz.ssh2.Connection.connect(Connection.java:603)3 more
Caused by: java.io.IOException: Cannot negotiate, proposals do not match.
at ch.ethz.ssh2.transport.KexManager.handleMessage(KexManager.java:411)
at ch.ethz.ssh2.transport.TransportManager.receiveLoop(TransportManager.java:604)
at ch.ethz.ssh2.transport.TransportManager$1.run(TransportManager.java:315)
at java.lang.Thread.run(Unknown Source)

解决方案:

这个报错是因为Connect.connect()在连接的时候 authenticated =为false,也就是说认证没有通过,导致的连接失败。

在这里插入图片描述
这是因为算法加密的不同导致的,我们可以在Linux下打开ssh的配置文件进行修改。

vim /etc/ssh/sshd_config

然后再最后面,添加该信息即可。

KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,[email protected]

在这里插入图片描述

最后不要忘记了重启sshd服务

systemctl restart sshd.service

运行成功

然后我们再运行下代码,我这边执行的shell命令是ls,即在root目录下,列出当前文件夹。发现完美解决。
在这里插入图片描述

写到最后,估计还有别人用过JSch,那么JSch和ganymed-ssh2的区别有哪些呢?

JSch的缺点:

学习成本高:JSch提供了非常丰富的API和选项,因此学习JSch的成本可能会比较高。
代码较为复杂:JSch的代码相对较为复杂,需要一定的技术水平才能使用和维护。

Ganymed-SSH2的缺点:
功能相对较少:Ganymed-SSH2的功能相对较少,不支持一些高级功能,如端口转发、SFTP和SCP等协议。

所以,大家如果只是偶尔用一次,不是经常性的使用,还是推荐使用Ganymed-SSH2插件,方便快捷,提供的基本功能也基本上够用

猜你喜欢

转载自blog.csdn.net/weixin_44427181/article/details/131454656