Need an example of SFTP with SSH 2.0

Oct 14, 2013 at 9:29 PM
Edited Oct 14, 2013 at 9:33 PM
I am trying to connect to an SFTP site that is using SSH-2.0-OpenSSH_5.6

I have not been able to connect with this:
ConnectionInfo connectionInfo = new PasswordConnectionInfo(host, Port, user, pass);
            
            using (var sftp = new SftpClient(connectionInfo))
            {
                sftp.Connect();    
                

                using (var file = File.OpenRead(localFileName))
                {
                    sftp.UploadFile(file, remoteFileName);
                }

                sftp.Disconnect();
            }
I think I need to use a different type of ConnectionInfo, but have not been able to figure this out.

Also, is an IP required for host, or will a URL work as well? All the examples I have found use IP only.

Also, I have searched in vain in the current download for the Samples, but I cannot find them. The documentation only gives the class info, no samples or how to's that I can find.

Also, I don't see how to enter the host key.

Greg
Oct 15, 2013 at 5:29 AM
gregoryagu wrote:
I am trying to connect to an SFTP site that is using SSH-2.0-OpenSSH_5.6

I have not been able to connect with this:
Why? What is happening?
ConnectionInfo connectionInfo = new PasswordConnectionInfo(host, Port, user, pass);
            
            using (var sftp = new SftpClient(connectionInfo))
            {
                sftp.Connect();    
                

                using (var file = File.OpenRead(localFileName))
                {
                    sftp.UploadFile(file, remoteFileName);
                }

                sftp.Disconnect();
            }
I think I need to use a different type of ConnectionInfo, but have not been able to figure this out.

Also, is an IP required for host, or will a URL work as well? All the examples I have found use IP only.
IP and Hostnames are valid.
Also, I have searched in vain in the current download for the Samples, but I cannot find them. The documentation only gives the class info, no samples or how to's that I can find.

Also, I don't see how to enter the host key.
Do you mean to validate the servers host key?
Oct 15, 2013 at 1:47 PM
Thanks very much.

Turns out I was not actually setting the Port. When I did, I was able to connect.

Greg
Oct 16, 2013 at 1:37 PM
da-rinkes, I have same issue, I need to validate host key. Can I configurate connection with host key check?
Oct 16, 2013 at 1:55 PM
SshClient has a HostKeyReceived-Event, where you get HostKeyEventArgs in the eventhandler.
HostKeyEventArgs contains the HostKey (byte-array) and has the boolean-flag CanTrust.

So you can validate the HostKey and set e.CanTrust to false (do not trust) and true (do trust).
If you set CanTrust to false the connection is cancelled.

To get the readable string of the e.HostKey I usually do: Convert.ToBase64String(e.HostKey);

Hope it helps
Oct 16, 2013 at 8:12 PM
da_rinkes, thank you! But unfortunately I still can't connect with "No suitable authentication method found to complete authentication." exception. (( but can do it alternative solutions like winscp.
This is my code:
using (var sftp = new SftpClient(new PasswordConnectionInfo(host, 22, name, password)))
{
    sftp.HostKeyReceived += sftp_HostKeyReceived;
    sftp.Connect();
}
Thanks anyway for your cool library!
Oct 17, 2013 at 6:25 AM
"No suitable authentication method found to complete authentication" usually means your password/username-combination is wrong.
SftpClient was unable to auth with the given credentials. Are you sure name and password are correct?

P.S.: It's not my library. I just use it very often and want to give back s.th. to the community :)
Oct 17, 2013 at 6:50 AM
Yes, I'm quite sure that my credentials are right, cause I have same host/port/name/password settings in acting with winscp dll and it works, but I want to use pure .net library instead of it.

P.S. da_rinkes, cool then you do it, it's really important thing in open-source!!!!
Oct 17, 2013 at 7:29 AM
Hm. I'm quite sure that has to be the error.

Just tried it and works OK here:
using System;
using System.Collections.Generic;
using Renci.SshNet;

namespace SshNetTests
{
    class Program
    {
        static void Main(string[] args)
        {
            var clients = new List<SftpClient>
            {
                new SftpClient(new PasswordConnectionInfo(Resource.host, 22, Resource.user, Resource.password)),
                new SftpClient(Resource.host, 22, Resource.user, Resource.password)
            };

            try
            {
                foreach (var sftpClient in clients)
                {
                    Console.Write(@"Connecting...");
                    using (var sftp = sftpClient)
                    {
                        sftp.Connect();
                        Console.WriteLine(@"Done.");
                        var sftpFiles = sftp.ListDirectory("/");
                        foreach (var sftpFile in sftpFiles)
                        {
                            Console.WriteLine(sftpFile.FullName);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            Console.ReadLine();
        }
    }
}
Oct 17, 2013 at 1:55 PM
Still doesn't work. Here is written winscp and ssh.net connect, and with both type of init SSH.Net throws "No suitable authentication method found to complete authentication", while winscp works fine (((
            var sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = host,
                UserName = name,
                Password = password,
                SshHostKeyFingerprint = fingerprint
            };

            using (var session = new Session())
            {
                session.Open(sessionOptions);
                var files = session.GetFileInfo("/");
            }

            var clients = new List<SftpClient>
            {
                new SftpClient(new PasswordConnectionInfo(host, 22, name, password)),
                new SftpClient(host, 22, name, password)
            };

            foreach (SftpClient sftpClient in clients)
            {
                try
                {
                    Console.Write(@"Connecting...");
                    using (SftpClient sftp = sftpClient)
                    {
                        sftp.Connect();
                        Console.WriteLine(@"Done.");
                        var sftpFiles = sftp.ListDirectory("/");
                        foreach (var sftpFile in sftpFiles)
                        {
                            Console.WriteLine(sftpFile.FullName);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }
            Console.ReadLine();
May 2, 2014 at 3:48 PM
DenisAgarev

Are you able to figure this out while using SSh.Net ?
Coordinator
May 5, 2014 at 6:11 PM
Edited May 5, 2014 at 6:44 PM
oceanindian:
can you be more specific ? do you want to verify the host key ?