6

I'm using the SSH.NET library to connect to a remote SFTP server. I'm trying to use very basic code but it's not working

using (var client = new SftpClient(host, username, password))
{
    client.Connect();   
    client.ChangeDirectory(@"sftp://server.example.com/other_directory");
}

However, this throws an exception saying No Such File on the ChangeDirectory method.

I tried the same with Curl but got an error saying

curl: (51) SSL peer certificate or SSH remote key was not OK

However, I added curl's --insecure argument and everything worked fine.

Could the --insecure part be related to why the SSH.NET library isn't working or is there another reason? Is there a way to simulate what --insecure does in C#?

Thanks

5
  • Sounds to me like sftp://server.example.com/ is denying you a secure connection. So it may be a configuration issue, and not anything you're doing wrong on the client side.
    – Max Sorin
    Commented Apr 1, 2016 at 11:59
  • I thought that may be the case at first but I'm able to access it via curl and other third party tools on my machine so I figured it would be on my side?
    – TryNCode
    Commented Apr 1, 2016 at 12:01
  • Capture data using a sniffer like wireshark or fiddler and compare results between working and non working applications. Usually the issue is difference in the html header(s).
    – jdweng
    Commented Apr 1, 2016 at 12:07
  • @jdweng Great idea, thanks!
    – TryNCode
    Commented Apr 1, 2016 at 12:08
  • Fixed it, answer posted below
    – TryNCode
    Commented Apr 1, 2016 at 12:24

2 Answers 2

9

If anyone else runs into this issue, it turns out that any of the methods such as ChangeDirectory and UploadFile expect a path relative to the WorkingDirectory property. As a result, I fixed my issue by changing it to

client.ChangeDirectory(@"/other_directory");

Hope that helps someone else

3
  • 1
    The /other_directory is an absolute path, not relative path. The point is the methods require paths, but you were giving URL instead. Commented Apr 12, 2016 at 7:10
  • I thought it's a relative path? It's relative to the working directory coffeecup.com/help/articles/absolute-vs-relative-pathslinks
    – TryNCode
    Commented May 20, 2016 at 15:27
  • 1
    That article is wrong. It says "paths", but it's about URLs actually. We talk about paths here, not URLs. A path relative to working directory does cannot start with slash. Commented May 20, 2016 at 19:53
0

In my case the issue was permissions for the user "username". var client = new SftpClient(host, username, password)

So the user "username" must have necessary permission on the folder other_directory for the following code to work correctly.

client.ChangeDirectory(@"/other_directory");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.