gpt4 book ai didi

java - SSH 命令执行失败并出现异常 - "net.schmizz.sshj.connection.ConnectionException: Connection reset Exception is thrown"

转载 作者:行者123 更新时间:2023-11-30 06:13:21 29 4
gpt4 key购买 nike

当通过 SSH 连接执行命令时,命令执行通常会失败并出现异常 - “net.schmizz.sshj.connection.ConnectionException:抛出连接重置异常”。

问题描述:

我使用 net.schmizz.sshj.SSHClient 创建了到主机的 SSH 连接实例。我使用此 SSH 连接执行一系列命令,最后关闭连接。

在检查我的java代码执行日志时,我经常会看到错误

2018-04-12T06:15:23.288Z ERROR n.s.s.t.TransportImpl [die:589] [reader] - Dying because - {}
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at net.schmizz.sshj.transport.Reader.run(Reader.java:50)
2018-04-12T06:15:23.288Z INFO n.s.s.t.TransportImpl [notifyDisconnect:182] [reader] - Disconnected - UNKNOWN

java代码执行将继续运行。

但是当使用SSH连接执行命令时,如果出现java.net.SocketException: Connection reset Exception,我的java代码执行就会终止。

 2018-04-11T12:02:51.755Z INFO  c.v.v.u.SSHUtil [executeRemoteSSHCommand:861] [main] - Successfully executed 'tar -C /usr/local/ -zxvf /usr/local/VMware-GuestSDK-10.2.0-8225092.tar.gz --no-same-owner' command on remote ssh host
2018-04-11T12:03:18.121Z ERROR n.s.s.t.TransportImpl [die:589] [reader] - Dying because - {}
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at net.schmizz.sshj.transport.Reader.run(Reader.java:50)
2018-04-11T12:03:18.123Z INFO n.s.s.t.TransportImpl [notifyDisconnect:182] [reader] - Disconnected - UNKNOWN
2018-04-11T12:03:18.127Z ERROR n.s.c.Promise [tryRetrieve:175] [main] - <<chan#6 / open>> woke to: net.schmizz.sshj.connection.ConnectionException: Connection reset
2018-04-11T12:03:18.130Z ERROR c.v.v.e.OutcomePrinter [afterInvocation:40] [main] - exception thrown
net.schmizz.sshj.connection.ConnectionException: Connection reset
at net.schmizz.sshj.connection.ConnectionException$1.chain(ConnectionException.java:32)
at net.schmizz.sshj.connection.ConnectionException$1.chain(ConnectionException.java:26)
at net.schmizz.concurrent.Promise.deliverError(Promise.java:96)
at net.schmizz.concurrent.Event.deliverError(Event.java:74)
at net.schmizz.concurrent.ErrorDeliveryUtil.alertEvents(ErrorDeliveryUtil.java:34)
at net.schmizz.sshj.connection.channel.AbstractChannel.notifyError(AbstractChannel.java:226)
at net.schmizz.sshj.connection.channel.direct.SessionChannel.notifyError(SessionChannel.java:224)
at net.schmizz.sshj.common.ErrorNotifiable$Util.alertAll(ErrorNotifiable.java:35)
at net.schmizz.sshj.connection.ConnectionImpl.notifyError(ConnectionImpl.java:258)
at net.schmizz.sshj.transport.TransportImpl.die(TransportImpl.java:597)
at net.schmizz.sshj.transport.Reader.run(Reader.java:68)
Caused by: net.schmizz.sshj.common.SSHException: Connection reset
at net.schmizz.sshj.common.SSHException$1.chain(SSHException.java:36)
at net.schmizz.sshj.common.SSHException$1.chain(SSHException.java:29)
at net.schmizz.sshj.transport.TransportImpl.die(TransportImpl.java:591)
... 1 common frames omitted
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at net.schmizz.sshj.transport.Reader.run(Reader.java:50)
2018-04-11T12:03:18.131Z INFO c.v.v.e.OutcomePrinter [afterInvocation:47] [main] - OUTCOME:FAIL

问题:

SSH 连接经常断开并重新连接,但是当我通过此 SSH 连接执行命令时,如果发生断开连接,它永远不会重新连接,而是 net.schmizz.sshj.connection.ConnectionException:抛出连接重置异常。

我的查询:

如何解决 Java 代码中的 ssh 断开连接问题,尤其是在通过 SSH 连接执行命令时?

谢谢。

最佳答案

来自javadoc:SocketException:抛出该异常表示创建或访问 Socket 时出错。

所以基本上,只要不是你的系统导致的,你就没有办法阻止这种情况。连接丢失。通常此错误是因为服务器出于某种原因断开了您的客户端连接。

您可以做的是,如果抛出错误,您只需让客户端重新连接即可。或者,如果这是用户操作的客户端,您可以监视并行线程中的连接,并在用户知道连接丢失之前使客户端重新连接。

关于java - SSH 命令执行失败并出现异常 - "net.schmizz.sshj.connection.ConnectionException: Connection reset Exception is thrown",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49796804/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com