SSH.NET连接问题

时间:2014-02-07 11:15:30

标签: c# ftp ssh.net

我试图使用SSH.NET库访问FTP服务器,没有任何运气。我提供与FileZilla相同的信用证,它们工作正常。 SSH抛出错误“套接字读取操作已超时”。如果我使用与下面相同的代码但没有指定端口:21我收到一个错误:“用户无法进行身份验证”。有人可以提供见解吗?

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

2 个答案:

答案 0 :(得分:1)

如果以下情况仍未解决,则以下代码适用于我

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

Ananth

答案 1 :(得分:1)

SSH工具(如Renci SSH)应在端口22(安全)上使用。如果要连接到端口21,则需要另一个库。我使用过FluentFTP,它不像Renci那样容易使用,但是可以完成工作。

这是一个代码示例,您可以用来将文件上传到服务器(版本19.1.2)。无论使用端口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();
                }
            }


        }