简体   繁体   English

SSH.NET 连接问题

[英]SSH.NET connection issue

I am trying to access the FTP server using SSH.NET library without any luck.我正在尝试使用 SSH.NET 库访问 FTP 服务器,但没有任何运气。 I am providing same credidentials as in FileZilla which are working fine.我提供与 FileZilla 中相同的凭证,这些凭证工作正常。 The SSH throws errow "Socket read operation has timed out". SSH 抛出错误“套接字读取操作已超时”。 If I use the same code as bellow but without specifying the port :21 I got an error : "User cannot authenitcated" .如果我使用与下面相同的代码但没有指定端口 :21 我得到一个错误:“用户无法验证”。 Can someone privide insights?有人可以提供见解吗?

string tempHost = @"ftp.mywebsite.com";
string tempUser = @"ftp@mywebsite.com";
string tempPassword = @"try123";

using (SftpClient sftpClient = 
       new SftpClient((ConnectionInfo)new PasswordConnectionInfo(tempHost,21, tempUser, tempPassword)))
        {
          sftpClient.Connect();
        }

If in case it's still unresolved for you, below code worked for me如果它仍然没有为您解决,下面的代码对我有用

using (var sftp = new SftpClient(host, userName, password))
            {
                sftp.Connect();                    
                //Do some operation
                sftp.Disconnect();
            }

Ananth阿南特

SSH tools (like Renci SSH) are meant to be used on port 22 (secure). SSH 工具(如 Renci SSH)旨在用于端口 22(安全)。 If you want to connect to port 21 you need another library.如果要连接到端口 21,则需要另一个库。 I have used FluentFTP which is not as easy to use as Renci, but gets the job done.我使用了FluentFTP ,它不像Renci那样容易使用,但可以完成工作。

Here is a code sample you can use to upload a file to a server (on version 19.1.2).这是一个代码示例,可用于将文件上传到服务器(版本 19.1.2)。 Regardless if you use port 21 or 22, I would highly recommend at least 500ms between write operations to give time to the server to breath.无论您使用端口 21 还是 22,我强烈建议在写入操作之间至少间隔 500 毫秒,以便让服务器有时间喘息。

using (FtpClient client = new FtpClient())
        {
            client.Host = ftpAddress;
            client.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
            client.Port = 21;
            client.Connect();
            using (Stream s = new FileStream(localPathWithFileName, FileMode.Open))
            {
                try
                {
                    //log.Info($"Uploading file...");
                    client.Upload(s, ftpFilePathWithFileName);
                    //log.Info($"File uploaded!");
                }
                catch(Exception e)
                {
                    //log.Info($"{e.StackTrace}");
                }
                finally
                {
                    client.Disconnect();
                }
            }


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM