Renci.SshNet:“服务器响应不包含ssh协议标识". [英] Renci.SshNet : "server response does not contain ssh protocol identification"

查看:1231
本文介绍了Renci.SshNet:“服务器响应不包含ssh协议标识".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WPF应用程序上使用Renci SSH.Net库,但在使用SFTP客户端时遇到问题.当用户尝试连接以从SFTP服务器下载某些文件时,他得到以下消息:

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:

服务器响应不包含ssh协议标识

Server response does not contain ssh protocol identification

这似乎与服务器无关,因为我能够在开发台式机和辅助笔记本电脑上正常连接和下载文件.相同的应用程序能够通过SSH连接并运行命令而不会出现问题,只是SFTP连接似乎是问题所在.我正在寻找有关从哪里开始进行故障排除的一些指导.

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.

SFTP的代码如下所示:

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);
        }
    }

下面的SSH代码:

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));
        }
    }

推荐答案

只有通过尝试重试连接才能为我自己解决.找不到确切的问题所在,但是多次出现此连接问题.

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.

示例:

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

这篇关于Renci.SshNet:“服务器响应不包含ssh协议标识".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆