Renci.SshNet : "server response does not contain ssh protocol identification"

Chris picture Chris · Aug 26, 2017 · Viewed 23.1k times · Source

I'm working with the Renci SSH.Net library on a WPF application and I'm having an issue with using the SFTP client. When the user tries to connect to download some files from the SFTP server he gets the message shown below:

Server response does not contain ssh protocol identification

It doesn't appear to be something specific with the server as I'm able to connect and download the files just fine on my development desktop and a secondary laptop. The same application is able to connect over SSH and run commands without issue, it's just the SFTP connection that appears to be the problem. I'm looking for a little guidance as to where to begin troubleshooting this.

Code for SFTP shown below:

void DownloadPlogs()
    {
        try
        {
            SftpClient SFTP;
            if (GV.UseCustomPort && GV.CustomPort > 0 && GV.CustomPort < 65535)
            {
                SFTP = new SftpClient(GV.IpAddress, GV.CustomPort, GV.Username, GV.Password);
            }
            else
            {
                SFTP = new SftpClient(GV.IpAddress, 22, GV.Username, "");
            }
            SFTP.Connect();

            DownloadDirectory(SFTP, "/PLOG", Directory.GetCurrentDirectory() + @"\PLOG");
            ZipFile.CreateFromDirectory("PLOG", String.Format("{0} - {1} PLOGS.zip", GV.IpAddress, DateTime.Now.ToString("yyyyMMddHHmmss")));
            Directory.Delete(Directory.GetCurrentDirectory() + @"\PLOG", true);

            SFTP.Disconnect();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error Getting PLOGS");
        }
    }

    void DownloadDirectory(SftpClient Client, string Source, string Destination)
    {
        var Files = Client.ListDirectory(Source);
        foreach (var File in Files)
        {
            if (!File.IsDirectory && !File.IsSymbolicLink)
            {
                DownloadFile(Client, File, Destination);
            }
            else if (File.IsSymbolicLink)
            {
                //Ignore
            }
            else if (File.Name != "." && File.Name != "..")
            {
                var Dir = Directory.CreateDirectory(System.IO.Path.Combine(Destination, File.Name));
                DownloadDirectory(Client, File.FullName, Dir.FullName);
            }
        }
    }

    void DownloadFile(SftpClient Client, Renci.SshNet.Sftp.SftpFile File, string Directory)
    {
        using (Stream FileStream = System.IO.File.OpenWrite(System.IO.Path.Combine(Directory, File.Name)))
        {
            Client.DownloadFile(File.FullName, FileStream);
        }
    }

Code for SSH below:

public SshConnection(string Host, int Port, string Username, string Password)
    {
        myClient = new SshClient(Host, Port, Username, Password);
        myClient.KeepAliveInterval = new TimeSpan(0, 0, 5);
        myClient.HostKeyReceived += myClient_HostKeyReceived;
        myClient.ErrorOccurred += myClient_ErrorOccurred;
    }

void myClient_ErrorOccurred(object sender, Renci.SshNet.Common.ExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.Message, "SSH Error Occurred");
    }

    void myClient_HostKeyReceived(object sender, Renci.SshNet.Common.HostKeyEventArgs e)
    {
        e.CanTrust = true;
    }

    public async void Connect()
    {
        Task T = new Task(() =>
        {
            try
            {
                myClient.Connect();
            }
            catch (System.Net.Sockets.SocketException)
            {
                MessageBox.Show("Invalid IP Address or Hostname", "SSH Connection Error");
            }
            catch (Renci.SshNet.Common.SshAuthenticationException ex)
            {
                MessageBox.Show(ex.Message, "SSH Authentication Error");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
                MessageBox.Show(ex.GetType().ToString());
                OnConnection(this, new ConnectEventArgs(myClient.IsConnected));
            }
        });

        T.Start();
        await T;
        if (T.IsCompleted)
        {
            OnConnection(this, new ConnectEventArgs(myClient.IsConnected));
        }
    }

    public void Disconnect()
    {
        try
        {
            myClient.Disconnect();
            OnConnection(this, new ConnectEventArgs(myClient.IsConnected));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace, ex.Message);
        }
    }

    public void SendData(string Data)
    {
        try
        {
            if (Data.EndsWith("\r\n"))
            {
                RunCommandAsync(Data, SshCommandRx);
            }
            else
            {
                RunCommandAsync(String.Format("{0}\r\n",Data), SshCommandRx);
            }
            //SshCommand Command = myClient.RunCommand(Data);
            //OnDataReceived(this, new DataEventArgs(Command.Result));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace, ex.Message);
        }
    }

    private async void RunCommandAsync(String Data, SshCommandCallback Callback)
    {
        Task<SshCommand> T = new Task<SshCommand>(() =>
        {
            try
            {
                SshCommand Command = myClient.RunCommand(Data);
                return Command;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
                return null;
            }
        });

        T.Start();
        await T;
        if (T.IsCompleted)
        {
            Callback(this, T.Result);
        }
    }

    private void SshCommandRx(SshConnection C, SshCommand Command)
    {
        if (Command != null)
        {
            string Rx = Command.Result;
            //if (Rx.StartsWith(Command.CommandText))
            //{
            //    Rx = Rx.Remove(0, Command.CommandText.Length);
            //}
            while (Rx.EndsWith("\r\n\r\n") == false)
            {
                Rx += "\r\n";
            }
            OnDataReceived(this, new DataEventArgs(Rx));
        }
    }

Answer

grinay picture grinay · Feb 20, 2018

I solve it for my self only with connections retrying attempts. Didn't find what exactly the issue is, but have this connection issue many times.

Example:

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