Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly getting Renci.SshNet.SftpClient.Connect throwing SshConnectionException

Tags:

c#

sftp

ssh.net

I've seen other threads about this error, but I am having this error randomly. Out of 30 connects, 12 got this error. Trying to understand why this is, and what possible solutions are.

using (SftpClient client = new SftpClient(sftpHost, sftpPort, sftpUser, sftpPassword))
{
    client.Connect();
}

throws this exception:

Renci.SshNet.Common.SshConnectionException: Server response does not contain SSH protocol identification

like image 417
AlgoPro Avatar asked Feb 04 '19 20:02

AlgoPro


1 Answers

No one knows the exact cause for this behavior to be honest.

As a possible fix, Some people suggest creating a counter loop for retrying to connect, suggesting it solved their problem:

int attempts = 0;
do
{
    try
    {
        client.Connect();
    }
    catch (Renci.SshNet.Common.SshConnectionException e)
    {
        attempts++;
    }
} while (attempts < _connectiontRetryAttempts && !client.IsConnected);

Another suggestion is to add the IP address into the hosts.allow file on the server, so you could check in that direction as well.

As I said, there is no possible explanation for the behavior altogether.

like image 130
Barr J Avatar answered Sep 16 '22 17:09

Barr J