11

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

Razkar
  • 539
  • 5
  • 26
  • 1
    Judging from [the source code](https://github.com/sshnet/SSH.NET/blob/bd01d971790a7c1fa73bad35b79ada90bf69e62d/src/Renci.SshNet/Session.cs#L1852) that would indicate general communication error, either timeout or connection drop. Are you able to connect to server using some regular SSH client? Is your connection stable? – orhtej2 Aug 26 '19 at 20:45
  • worst part is that i'm getting this error totally unpredictably, sometimes 10% of the time, sometimes 50% – Alex Gordon Aug 26 '19 at 20:59
  • 2
    @l--''''''---------'''''''''''' that sounds like a network issue. If you use putty/sftp or other ssh/sftp client what are the results? If you do a continous `ping` (over at least hour) what does it show? – tukan Aug 27 '19 at 07:53
  • 2
    what does the sniffer indicate the differences in a good and bad response? SSH is a secure shell connection using TCP port 22. Something in the encryption is probably failing but not sure without seeing sniffer data. – jdweng Aug 27 '19 at 09:58
  • I think the error should be caused by the ftp server(may be busy etc). Check maximum connections setting of the server. – Gokhan Aug 31 '19 at 10:39

2 Answers2

8

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:

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
1

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.

Milo
  • 3,365
  • 9
  • 30
  • 44
Barr J
  • 10,636
  • 1
  • 28
  • 46