sftp exception : The session was closed by the remote connection end

0 votes
asked Aug 17, 2015 by Sathyabama (140 points)

While connecting to the sftp server using public key authentication, we are getting following error.

The session was closed by the remote connection end. Make sure you are connecting to an SSH or SFTP server. ---> Rebex.Net.SshException: The session was closed by the remote connection end. Make sure you are connecting to an SSH or SFTP server.

Need more details on this.

1 Answer

0 votes
answered Aug 17, 2015 by Lukas Pokorny (102,250 points)

This error is thrown by Sftp object's Connect method, which means that it occurred before public key authentication was attempted. It actually occurred even before a secure SSH session was established.

Please create a communication log using Sftp object's LogWriter property, but use LogLevel.Verbose level instead of LogLevel.Debug. Check the log to make sure it doesn't contain any sensitive information (such as passwords, although none should be there if the error occured before authentication) and either mail it to support@rebex.net or post it here. Based on the log, we should be able to tell what is going on. Thanks!

commented Aug 18, 2015 by Sathyabama (140 points)
Thanks for your reply. I will try to get the communication log. mean while can you please verify if the following stack trace will help you out in identifying the issue?

Rebex.Net.SftpException: The session was closed by the remote connection end. Make sure you are connecting to an SSH or SFTP server. ---> Rebex.Net.SshException: The session was closed by the remote connection end. Make sure you are connecting to an SSH or SFTP server.
   at Rebex.Net.SshSession.14Ad4fZ()
   at Rebex.Net.SshSession.Negotiate()
   at Rebex.Net.Sftp.Connect(String serverName, Int32 serverPort, SshParameters parameters)
   --- End of inner exception stack trace ---
   at Rebex.Net.Sftp.Connect(String serverName, Int32 serverPort, SshParameters parameters)
   at Rebex.Net.Sftp.Connect(String serverName)
commented Aug 18, 2015 by Lukas Pokorny (102,250 points)
The most common cause of this error is trying to connect to an endpoint that is not an SFTP/SSH server. For example, trying to connect to an FTP/SSL server at test.rebex.net:990 triggers the same error - try this: sftp.Connect("test.rebex.net", 990);

Are you able to connect to the SFTP server at test.rebex.net? Try this: sftp.Connect("test.rebex.net", 22);
commented Aug 19, 2015 by Sathyabama (140 points)
Hi I am yet to try with test.rebex.net.. But one point what I want to mention here is, this is not a consistent issue. For the same code and for the same environment, it works most of the time without any issues. So I am sure it is connecting to SFTP server...
commented Aug 19, 2015 by Sathyabama (140 points)
We have a scheduled task that runs at configured timings... we will first read a file from the sftp server... then we send few files to the server... So in this scenario, when we first try to connect to sftp for reading a file, it fails with the above mentioned exception. We have a retry logic there. so it retries again after 10 mins.. in the second try, we are not getting any exception but the file is not read properly so it throws some null exception. But this time as we don't have any ftp exception, it continues to send a file to the ftp server which is successfully sent. So only at the first time it is giving this exception...
commented Aug 19, 2015 by Lukas Pokorny (102,250 points)
Perhaps the server is occasionally rejecting incoming connections for some reason? Issues like this are hard to analyze without access to your infrastructure... Possible actions to take now include:
a) Try using a third-party command-line SFTP client such as psftp (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) to connect to the server every few minutes (using a simple batch script, for example). If it occasionally fails as well, it means the issue is not caused by the client.
b) Install a network protocol analyzer such as Wireshark (at the machine where the SFTP client runs) and capturing all SSH traffic - then, once the error occurs again, you would be able to see which side of the connection is responsible for the failure.
c) Get in touch with the SFTP server administrators and ask them to look into the server log - it might contain the reason for closing the connection.
commented Aug 19, 2015 by Lukas Pokorny (102,250 points)
Also, please post the exception details for the "null exception" you get during the second try as well.
commented Sep 7, 2015 by Sathyabama (140 points)
Hi, the null exception was a problem with our code. we handled it.

After few days we got the exception only yesterday. Please find below the verbose log.

When connected successfully

2015-09-04 12:10:34.021 Opening log file.
2015-09-04 12:10:34.318 INFO Sftp(4)[1] Info: Connecting to www.####.com:22 using Sftp 3.0.4444.0.
2015-09-04 12:10:34.474 DEBUG Sftp(4)[1] SSH: Server is 'SSH-2.0-OpenSSH_4.3'.
2015-09-04 12:10:34.474 INFO Sftp(4)[1] SSH: Negotiation started.
2015-09-04 12:10:34.474 VERBOSE Sftp(4)[1] SSH: Sending packet SSH_MSG_KEXINIT

During problem(on retry it worked):

2015-09-04 12:00:05.426 Opening log file.
2015-09-04 12:00:05.441 INFO Sftp(1)[1] Info: Connecting to www.####.com:22 using Sftp 3.0.4444.0.
2015-09-04 12:00:05.660 ERROR Sftp(1)[1] SSH: Rebex.Net.SshException: The session was closed by the remote connection end. Make sure you are connecting to an SSH or SFTP server.
   at Rebex.Net.SshSession.14Ad4fZ()
   at Rebex.Net.SshSession.Negotiate()
2015-09-04 12:00:05.675 ERROR Sftp(1)[1] Info: Rebex.Net.SshException: The session was closed by the remote connection end. Make sure you are connecting to an SSH or SFTP server.
   at Rebex.Net.SshSession.14Ad4fZ()
   at Rebex.Net.SshSession.Negotiate()
   at Rebex.Net.Sftp.Connect(String serverName, Int32 serverPort, SshParameters parameters)
2015-09-04 12:10:06.159 INFO Sftp(1)[1] Info: Connecting to www.####.com:22 using Sftp 3.0.4444.0.
2015-09-04 12:10:06.393 DEBUG Sftp(1)[1] SSH: Server is 'SSH-2.0-OpenSSH_4.3'.
2015-09-04 12:10:06.393 INFO Sftp(1)[1] SSH: Negotiation started.
commented Sep 7, 2015 by Tomas Knopp (58,890 points)
Hello,

thank you for the verbose log. Actually, the problem is not at the Rebex SFTP client side. From the client  point of view your server behaves unreliably. Sometimes it accepts the connection and sometimes it behaves as if it wasn't a SFTP server at all.

You can try it yourself:
if you connect to the normally working SFTP server with Telnet client, you will see a server reply in this format "SSH-xyz". However, in case of your above error, the server does not announce itself as expected and instead it closes the connection. So the only way we can handle it is throw an exception.

From your description and the fact that the error is only raised occasionally and most of the time the SFTP server works correctly, it looks like a temporary server problem that occurs from time to time.

For sure, it would be best to ask the server administrators for a help and report this problem to them. They might not even be aware of such behaviour.

If you cannot solve it at the server side, then a typical way to solve this occasional server issue is to retry the Connect method several times, before finally giving up. It would be good to wait a certain amount of time in your program (something like 30 or 60 seconds). In the meantime it is possible that the server will recover.
...