3

When a command is executed via SSH connection, often the command execution fails with Exception - "net.schmizz.sshj.connection.ConnectionException: Connection reset Exception is thrown".

Problem description:

I created a SSH connection instance to a host using net.schmizz.sshj.SSHClient. I execute bunch of commands using this SSH connection and finally close the connection.

On checking my java code execution logs, Often I could see the error

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

The java code execution will continue to run.

But when executing a command using the SSH connection, in that time if this java.net.SocketException: Connection reset Exception comes, my java code execution is terminated.

 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

Issue:

often the SSH connection disconnects and reconnects but when I execute a command via this SSH connection and in that time if the disconnect happens, it never reconnects instead net.schmizz.sshj.connection.ConnectionException: Connection reset Exception is thrown.

My Query:

How to overcome this ssh disconnection issue from my Java code especially while executing commands via the SSH connection ?

Thanks.

Vijay Kumar R
  • 41
  • 1
  • 1
  • 4
  • A connection reset usually means the remote server disconnected the TCP session. You need to troubleshoot this on the remote system to figure out why it's happening. – Kenster Apr 12 '18 at 13:46

2 Answers2

2

From javadoc: SocketException: Thrown to indicate that there is an error creating or accessing a Socket.

So basically you don't have way to prevent that as long as it's not your system causing eg. connection drops. Usually this error is because the server from whatever reasons is disconnecting your client.

What you might be able to do is that you simply make the client reconnect if error is thrown. Or if this is an user operated client you could monitor the connectivity in parallel thread and make the client reconnect before user even knows the connection was lost.

Jokkeri
  • 1,001
  • 1
  • 13
  • 35
0

Could be because of ssh host verification. Try to ssh from this server first so that it's added to known hosts.

DimiDak
  • 4,820
  • 2
  • 26
  • 32