This project is read-only.

First attempt at SSH/SFTP in C#

Jul 10, 2011 at 9:59 AM

Hi guys.
I'm pretty much the C# n00b but I was given the task to implement a small SFTP utility to download files from a parent company's server.

My idea was to connect via SSH, list the files that I will need to transfer, disconnect, store the names in an array and then download them (via sftp) using a foreach loop. Would this be the best way to proceed?

Also, could anyone please provide me with an example for the filetransfer? I looked in the documentation but didn't find anything relevant.
Thanks!

Max

Jul 10, 2011 at 3:26 PM

Hi

Yes, browse the source code, and look in the tests drectory, thats the best "documentation" at the moment.

Good luck!

On 10. juli 2011, at 10:59 AM, "mhaber" <notifications@codeplex.com> wrote:

From: mhaber

Hi guys.
I'm pretty much the C# n00b but I was given the task to implement a small SFTP utility to download files from a parent company's server.

My idea was to connect via SSH, list the files that I will need to transfer, disconnect, store the names in an array and then download them (via sftp) using a foreach loop. Would this be the best way to proceed?

Also, could anyone please provide me with an example for the filetransfer? I looked in the documentation but didn't find anything relevant.
Thanks!

Max

Jul 11, 2011 at 9:16 AM

Thanks for the tips!
Stupid question: how do I build the docs?

 

Max

Jul 11, 2011 at 9:19 AM

Never mind ;)

Max

Jul 11, 2011 at 10:56 AM

It seems that I can't execute a shell when connecting to the remote SSH server, so my idea to list the files I'll need to download won't work.
Putty's log:

2011-07-11 11:50:46    Server version: SSH-2.0-OpenSSH_5.3
2011-07-11 11:50:46    We claim version: SSH-2.0-PuTTY_Release_0.60
2011-07-11 11:50:46    Using SSH protocol version 2
2011-07-11 11:50:46    Doing Diffie-Hellman group exchange
2011-07-11 11:50:49    Doing Diffie-Hellman key exchange with hash SHA-256
2011-07-11 11:50:50    Host key fingerprint is:
2011-07-11 11:50:50    ssh-rsa 1024 4d:6e:45:6f:f2:5d:94:26:d0:cc:06:95:c6:4b:79:f4
2011-07-11 11:50:50    Initialised AES-256 SDCTR client->server encryption
2011-07-11 11:50:50    Initialised HMAC-SHA1 client->server MAC algorithm
2011-07-11 11:50:50    Initialised AES-256 SDCTR server->client encryption
2011-07-11 11:50:50    Initialised HMAC-SHA1 server->client MAC algorithm
2011-07-11 11:50:50    Reading private key file "C:\INVIK-PF.ppk"
2011-07-11 11:50:50    Pageant is running. Requesting keys.
2011-07-11 11:50:50    Pageant has 0 SSH-2 keys
2011-07-11 11:50:50    Configured key file not in Pageant
2011-07-11 11:50:50    Writing new session log (raw mode) to file: C:\putty.log
2011-07-11 11:50:54    Offered public key
2011-07-11 11:50:54    Offer of public key accepted
2011-07-11 11:50:54    Access granted
2011-07-11 11:50:54    Opened channel for session
2011-07-11 11:50:54    Allocated pty (ospeed 38400bps, ispeed 38400bps)
2011-07-11 11:50:54    Started a shell/command

 

Any other way to know what files are in a specified directory so I can download them?

Max

Jul 11, 2011 at 12:54 PM

Hi

You can use the SftpClient class for this.

See example code in

  • Renci.SshClient
    • Renci.SshClient.Tests
      • SftpClientTests
        • UploadDownloadFileTest.cs
        • ListDirectoryTest.cs

Sample code:

using (var sftp = new SftpClient("host", "username", "password"))
{
	var files = sftp.ListDirectory("/path/to/remote/directory");
	foreach (var file in files)
	{
		using (var stream = new File.OpenWrite("path to file on your system")) // you can also use file.Name.
		{
		    Debug.WriteLine("Downloading file \"" + file.FullName + "\"");
		    sftp.DownloadFile(file.FullName, ms);
		}
	}
}