3

I want to use SharpSSH to upload a file to a SFTP server.

I got SharpSSH.dll, the file to upload, a public key and I sent the private key to the server. They gave me a username and no password is needed.

I tried this:

Sftp sftp = new Sftp(ip, user);
sftp.Connect();
sftp.Put(filePath, toPath);
sftp.Cancel();

Do I need a HostKey somewhere here and if yes, where would I have to put it in, and how do I make one out of a .ppk file?

2
  • Hi, What is the format for sftp.Put toPath? Im connected to my sftp server but I cant upload to an specific folder in my server Commented Aug 7, 2014 at 8:54
  • I think you meant that you uploaded the public key on the server and kept the private key on the machine where the C# code runs. Commented Jan 4, 2015 at 18:56

2 Answers 2

1

First things first, your key terms are back-to-front, or at least I hope they are. You send the public key out, and keep the private key safe and secure.

Aside from that, yes, with SharpSSH you need to include the private key location.

sftp.AddIdentityFile("path/to/identity/file");

If your key has a password, then use the overloaded version, i.e.

sftp.AddIdentityFile("path/to/file", "password");

The key file itself, I believe, needs to be in OpenSSH format.

I'm also not sure about your inclusion of sftp.Cancel(); Would it not be better to enclose you connect and Put commands into a try/catch/finally block, and call sftp.close() within the finally block?

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a my solution:

using Tamir.SharpSsh;
using Tamir.SharpSsh.jsch;

Method code:

public static bool SftpFile(string ftpAddress, string username, string password, string port, string folderPath, string filename, string separator, string keyFilename, string keyPassword)
{
    bool Success = false;
    Sftp sftp = null;
    try
    {

        if (filename.Length > 0 && dt != null)
        {
            //Send file

            int NumberOfConnectionAttempts = 0;

        JumpPoint:

            try
            {
                sftp = new Sftp(ftpAddress, username);

                sftp.Password = password;

                sftp.AddIdentityFile(keyFilename, keyPassword);

                // non-password alternative is sftp.AddIdentityFile(keyFilename);

                sftp.Connect();

                sftp.Put(filename + ".csv", (!String.IsNullOrWhiteSpace(folderPath) ? folderPath + "/" : "") + filename + ".csv");

                Success = true;

            }
            catch (Exception ex)
            {
                Program.DisplayText(" Connection " + NumberOfConnectionAttempts + " failed.\n");

                if (NumberOfConnectionAttempts < Program.IntTotalAllowedConnectionAttempts)
                {
                    NumberOfConnectionAttempts++;

                    Thread.Sleep(1000);

                    goto JumpPoint;

                }
                else
                {
                    //Program.HandleException(ex);

                }
            }
        }

    }
    catch (Exception ex)
    {
        //Program.HandleException(ex);
    }
    finally
    {
        //Close sftp

        try { sftp.Close(); }
        catch { }

        try { sftp = null; }
        catch { }

        try { GC.Collect(); }
        catch { }

    }
    return Success;
}

Usage example:

string FtpAddress = "??.??.??.??";

string Port = "21";

string Username = "my-username";

string Password = "P455w0rd";

string FolderPath = "folder-name\\"; 

string Filename = "filename.foo";

string KeyFilename = "keyFilename.bar";

string KeyPassword= "K3yP455w0rd";

if (SftpFile(FtpAddress, Username, Password, Port, FolderPath, Filename, ",", KeyFilename, KeyPassword))
{
    /* Success */

}
else
{
    /* Error */

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.