abc
abc

Reputation: 2437

How to upload a file to SFTP server using SharpSSH

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?

Upvotes: 3

Views: 23522

Answers (2)

WonderWorker
WonderWorker

Reputation: 9092

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 */

}

Upvotes: 0

R Kelly
R Kelly

Reputation: 11

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?

Upvotes: 1

Related Questions