0

I get Renci.SshNet.Common.SshOperationTimeoutException when using SftpClient, currently i only able to use SshClient, is there a way to download and upload file using SshClient? Could any one shed some light on this?

I tried ftp 10.101.10.10 in window command prompt, it works flawlessly. File is 1KB only.

//this code gives Renci.SshNet.Common.SshOperationTimeoutException
using (var sftp = new SftpClient("10.101.10.10", "user", "passcode"))
{
    sftp.Connect(); 
    sftp.Timeout = new TimeSpan(0, 0, 60);
    sftp.Disconnect()
}


//This code work!
using (var client = new SshClient("10.101.10.10", "user", "passcode"))
{
    client.Connect();
    //i did tried with some other list file command here, it work also, but i don't know how to download and upload file
    client.Disconnect();
}
Ong Ming Soon
  • 1,041
  • 1
  • 10
  • 22
  • You should read [ask]. – Enigmativity Jul 09 '18 at 08:20
  • 1
    Yes there is using the appropriate methods. Where's your code though? What did you try, what is the *exact* error? Did you get a timeout while opening a connection? Or are you trying to download a big file over a slow connection? – Panagiotis Kanavos Jul 09 '18 at 08:22
  • BTW there are many duplicate questions [like this one](https://stackoverflow.com/questions/23703040/download-files-from-sftp-with-ssh-net-library). If you tried something similar and got an exception, post your code and *the full exception* including the call stack. You can get that easily using `Exception.ToString()`. The call stack shows which method actually threw the exception (Connect? ListDirectory? DownloadFile?) and what the exception was about. – Panagiotis Kanavos Jul 09 '18 at 08:24
  • Did you try using port **22**? Not sure what the default port is for the client, but the SFTP protocol itself often is configured to use port 22. – Peter B Jul 09 '18 at 09:15
  • The windows command prompt doesn't work with SFTP servers, only FTP or FTPS - it's likely therefore that your server doesn't understand the SFTP connection request. The `SshClient` is only connecting to the port, so should succeed, until you attempt to do anything further. – Zhaph - Ben Duguid Jul 09 '18 at 09:21

1 Answers1

3

Not sure why, but i did tried WinSCP, and it working.

        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                FtpSecure = FtpSecure.None,
                HostName = "10.101.10.10",
                UserName = "user",
                PortNumber = 21,
                Password = "passcode",  
            };

            using (WinSCP.Session session = new WinSCP.Session())
            {
                // Connect
                session.Open(sessionOptions);
                var files = session.EnumerateRemoteFiles("/path/", "test*", WinSCP.EnumerationOptions.None);
                var directory = session.ListDirectory("/path/");

                foreach (var dir in files) {
                    Console.WriteLine(dir);
                }

                    // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                transferResult =
                    session.GetFiles("/path/test*", @"D:\", false, transferOptions);

                // Throw on any error
                transferResult.Check();

                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                }
            }

        }
        catch (Exception e)
        {
            Console.Write
Ong Ming Soon
  • 1,041
  • 1
  • 10
  • 22
  • Well, you are using FTP with WinSCP, while you tried SFTP with SSH.NET. Those are two completely different protocols. – Martin Prikryl Jul 09 '18 at 16:00
  • Worked for me! just got SshHostKeyFingerprint errors so I added GiveUpSecurityAndAcceptAnySshHostKey = true to the list of options under the SessionOptions definitions – Avishay Feb 19 '20 at 16:49