Razkar
Razkar

Reputation: 569

Randomly getting Renci.SshNet.SftpClient.Connect throwing SshConnectionException

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

Upvotes: 12

Views: 15899

Answers (3)

Frank Hagenson
Frank Hagenson

Reputation: 1033

I know this is an old thread, but to help anyone else who stumbles on it Like I did, I had this problem when connecting to Ubiquiti radios. The problem turned out to be that I had many ssh sessions to the radio running at once due to a bug in my code. Either the client or server was resource starved I think. Fixing the bug in my code made the error go away.

Upvotes: 1

Maytham Fahmi
Maytham Fahmi

Reputation: 33437

This might sound trivial, but I have tried the Renci.SshNet.Async package and it worked with out any issue. The following could be a reason/s for the problem with solution or alternative solution.

  1. Renci.SshNet.Async might have bug or issues that appears in specific environment or in specific combination. Some mentioned same problems here, no solution https://github.com/sshnet/SSH.NET/issues/377
  2. Some has experienced same problem and mentioned a solutions, one is to retry n number of times Renci.SshNet : "server response does not contain ssh protocol identification"
  3. The problem is not with your client but more or less with your Host. It can be many things or issues.
  4. Finally I would try using WinSCP which is also well recognized library for .net (nuget). I have tried it my self as well and have never experienced any issues so far. I suggest you to get it and try it as alternative to your current solution, and see if it helps. If WinSCP works then point 1 is valid, if you experience same issue with WinSCP then point 3 is valid. This way we can make conclusions and narrow down to the problem.

Here examples provided by WinSCP https://winscp.net/eng/docs/library_examples.

My Test example is:

This is just playground example to see if things are running, when used in production, please modify it. Further more thanks to @MartinPrikry adding this note: If you have set SshHostKeyFingerprint correctly, then do not set GiveUpSecurityAndAcceptAnySshHostKey.

public static int WinSCPListDirectory()
{
    try
    {
        var sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = "xxx.xxx.xxx.xxx",
            UserName = "username",
            Password = "password",
            SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
            GiveUpSecurityAndAcceptAnySshHostKey = true
        };

        using (var session = new Session())
        {
            session.Open(sessionOptions);

            var directory = session.ListDirectory("/var/www");

            foreach (RemoteFileInfo fileInfo in directory.Files)
            {
                Console.WriteLine(
                    "{0} with size {1}, permissions {2} and last modification at {3}",
                    fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
                    fileInfo.LastWriteTime);
            }
        }

        return 0;
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e);
        return 1;
    }
}

This example should return a list of directories

Related links:

Upvotes: 10

Barr J
Barr J

Reputation: 10927

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.

Upvotes: 1

Related Questions