简体   繁体   中英

SSH.NET connection issue

I am trying to access the FTP server using SSH.NET library without any luck. I am providing same credidentials as in FileZilla which are working fine. The SSH throws errow "Socket read operation has timed out". If I use the same code as bellow but without specifying the port :21 I got an error : "User cannot authenitcated" . 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). If you want to connect to port 21 you need another library. I have used FluentFTP which is not as easy to use as Renci, but gets the job done.

Here is a code sample you can use to upload a file to a server (on version 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.

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


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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