当通过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:连接重置异常,那么我的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:连接重置抛出异常。< / p>
我的查询:
如何克服我的Java代码中的ssh断开连接问题,尤其是在通过SSH连接执行命令时?
感谢。
答案 0 :(得分:0)
来自javadoc: SocketException:抛出该异常以指示创建或访问Socket时发生错误。
所以基本上,只要不是由您的系统引起的,就没有办法阻止它。连接断开。通常,此错误是由于服务器出于任何原因断开了客户端的连接。
您可能能够做的就是,如果抛出错误,您只需使客户端重新连接即可。或者,如果这是用户操作的客户端,则可以在并行线程中监视连接,并在用户甚至不知道连接丢失之前重新连接客户端。
答案 1 :(得分:0)
可能是因为ssh主机验证。尝试先从此服务器ssh,以便将其添加到已知主机。