SSH/SFTP Communications in C#
-
Saturday, September 24, 2005 8:12 PMI'm looking to get started implementing a SFTP handler in my application (.net 2.0) and so far I'm just not finding what I'm looking for. Seems like all the SSH/SFTP code and libraries out there are for the Linux crowd.
Can anyone point me in the right direction? Am I going to be making calls to a command-line SSH app, or is this something that's going to be doable with reasonable effort?
Answers
-
Sunday, September 25, 2005 2:55 AM
Microsoft does not provide thouse in-box.
You need to search for third-party one.
For example for SSH you can try this OpenSource http://www.routrek.co.jp/en/product/varaterm/granados.html as well there are a lot of others commertial vendors.
All Replies
-
Saturday, September 24, 2005 9:42 PMModerator
SSL/FTP is already supported in System.Net 2.0
You can also use Sockets/NetworkStream
and then compose an SSLStream on top of the NetworkSteam.
That way you don't need to worry about the SSL implementation- Proposed As Answer by Mehmood Ahmed Tuesday, September 07, 2010 11:08 AM
-
Sunday, September 25, 2005 1:45 AMSSH is not SSL, and SFTP is not FTP over SSL.
-
Sunday, September 25, 2005 2:54 AMModeratorI don't believe .NET supports ssh natively ... Mono does, but I think these were added specifically for linux.
http://www.jscape.com/articles/sftp_using_csharp.html
appears to be what you'd be looking for. There are other commercial .NET packages for ssh as well.
Josh -
Sunday, September 25, 2005 2:55 AM
Microsoft does not provide thouse in-box.
You need to search for third-party one.
For example for SSH you can try this OpenSource http://www.routrek.co.jp/en/product/varaterm/granados.html as well there are a lot of others commertial vendors.
-
Sunday, September 25, 2005 10:01 PMthanks TAG I hadn't seen that one before, I'll look into it.
-
Monday, October 17, 2005 1:26 PM
Hi,
Recently I ported some of the JSch java SSH lib to C#. I posted my code at the codeproject website: http://www.codeproject.com/useritems/sharpSsh.asp.
The demo project contains an examples directory that also contains an SFPT implementation.- Proposed As Answer by jeganinfo Thursday, July 30, 2009 1:13 PM
-
Wednesday, December 07, 2005 7:23 PMhttp://www.tamirgal.com/home/dev.aspx?Item=sharpSsh
Is another link to this project. I just tested it and it works great for transfering SFTP files. -
Monday, November 06, 2006 11:53 PM
Hi Tamir
I'm wanting to transfer files over sftp in from an asp.net website to an sftp server. Will your classes adapt for this task? ie. Running in a remote host envirmonment as opposed to a windows app environment?
Any advice much appreciated.
Cheers.
-
Monday, February 19, 2007 3:46 PM
TAG wrote: You need to search for third-party one.
For example for SSH you can try this OpenSource http://www.routrek.co.jp/en/product/varaterm/granados.html as well there are a lot of others commertial vendors.
This product (Granados) is SSH only, not SFTP. Anyone wanting to implement SFTP should consider extending Granados. (1) A combined SSH/SFTP product is far more useful than either one alone because the user does not need to learn two totally different ways to interface into these related protocols. (2) A lot of the functionality (such as all the crypto) is already there and you can focus on producing the file transfer functionality.
-
Sunday, March 25, 2007 8:37 PM
Scott C. Reynolds wrote: I'm looking to get started implementing a SFTP handler in my application (.net 2.0) and so far I'm just not finding what I'm looking for. Seems like all the SSH/SFTP code and libraries out there are for the Linux crowd.
Can anyone point me in the right direction? Am I going to be making calls to a command-line SSH app, or is this something that's going to be doable with reasonable effort?There's a number of SSH/SFTP solutions for .NET.
You can check the comparison chart at http://www.eldos.com/sbb/sftpcompare.php . See the sample of how to use SFTP with VB.NET on http://www.eldos.com/sbb/sftp.net/sftp-net-vb-sample.php
Sincerely yours,
Eugene Mayevski
-
Tuesday, March 27, 2007 8:20 PMCan you direct me to where in Mono ssh is supported natively? I need ssh and maybe scp. I realize I could use one of the other solutions mentioned in this thread, but if it is possible to use Mono's libraries, that would be much more preferable.
Thanks! -
Thursday, May 10, 2007 10:03 PMRebex SFTP for .NET / .NET CF will also do the job. The interface seems to be easy-to-learn - see following example taken from SFTP component tutorial page.
// create client, connect and log in
Martin Vobr
Sftp client = new Sftp();
client.Connect("sftp.example.org");
client.Login("username", "password");
// select the desired directory
client.ChangeDirectory("path");
// retrieve and display the list of files and directories
SftpItemCollection list = client.GetList ();
foreach (SftpItem item in list)
{
Console.Write(item.Modified.ToString("u"));
Console.Write(item.Size.ToString().PadLeft(10,' '));
Console.Write(" {0}", item.Name);
Console.WriteLine();
} -
Monday, June 11, 2007 8:29 AMHello Tamir,
I am using your SharpSsh, but it doesn't work with commands for removing files. I tried to do it with SshExec and with SshShell by running a command "rm -f filename.wav", but didn't achieve to get a response. The command starts and never ends... Any idea?
Please answer as soon as possible.
Regards,
L.
I also posted the same question on code project, but nobody answered... -
Friday, August 17, 2007 9:30 AM
i tried with the Sharp SSH componet . it works to implementconnection with remote server but while upload/download files i am getting error like "unknown error"
- Proposed As Answer by while1 Wednesday, December 14, 2011 3:54 PM
-
Tuesday, September 11, 2007 11:36 PM
Hey Lenka,
I had similar problems trying to use SharpSsh and, so I took a dive into Granados and after some effort I could make it work
.What I do in my app is to connect to a list of servers(one at a time) and execute a series of non-interactive commands(one at a time also), it might be helpfull, are u interested?
Regards,
-
Friday, September 21, 2007 9:37 PM
Can you please share it? I am looking for FTP over SSH implementation from quite some time.
Thanks,
-
Friday, September 21, 2007 9:59 PM
I'm not using SFTP services (as a matter of fact I don't think Gradanos supports SFTP), only SSH to execute commands on a remote unix server. If u are still interested in my code plz let me know and I'll send it you
Regards
-
Saturday, September 22, 2007 4:08 PM
Can you please share it? I am working on making a C# SFTP component.... I would like to see how I can use this.
Thanks
-
Monday, September 24, 2007 11:25 PMDreico,
I am interested in seeing how you accomplished that with Granados, I'm working on something very similar and got it working in SharpSSH but the connection seems flaky. I'm having trouble getting a grasp on what to do in Granados, any help would be most appreciated.
Thanks,
Brian -
Wednesday, September 26, 2007 2:04 PM
If you're still having trouble, try IPWorks SSH: http://www.nsoftware.com/ipworks/ssh/.
If you have any questions feel free to contact me personally on my blog, I can help. Disclaimer: yes, I work at /n software.
Lance
-
Wednesday, September 26, 2007 2:10 PM
Hi Brian,
How can I help you? do you want me to send you my code? If so, plz post your email address and I'll do it.
Regards
-
Saturday, December 29, 2007 12:29 AM
.Net 2.0 only supports download for FTPS (aka FTP over SSL).
If you try to issue, for example, the Upload command, you will get a not supported issue.
It also has limitations regarding proxies.
-
Saturday, December 29, 2007 2:07 AMThat doesn't make any sense.
-
Friday, January 11, 2008 4:52 PMDreico,
Could you please send me your code examples. I'm new to C# (been C/C++ for years) and I'm having trouble wading through the Granados code.
My email address is madwhit@hotmail.com.
Thanks, I really appreciate it.
-Don
<>< -
Thursday, January 17, 2008 2:56 AM
-
Thursday, January 17, 2008 8:33 PMCheul,
I found Dreico's code here http://www.msdner.com/dev-archive/219/12-40-2190761.shtm
I can get it to login but I can't seem to get it to issue commands on the server. If I figure something more out I'll post here. Please do the same for me.
Hope this helps,
-Don
<>< -
Tuesday, January 20, 2009 8:24 PM
I am trying to do exactly the samething, did you find any solution to implement SFTP in C#
-
Tuesday, February 03, 2009 10:42 PMCan you please share your code if you got it working ?
Thanks
-
Tuesday, February 03, 2009 10:43 PMCan you please share your code if you got it working ?
Thanks
-
Friday, April 24, 2009 4:22 AM
Take a look at edtFTPnet/PRO, which is a comprehensive client library supporting SFTP, FTP and FTPS. Loads of features and it's been around for a long time.
-
Tuesday, June 02, 2009 7:23 AMI have prepared guide to using WinSCP SFTP client from .Net (including C# example). I hope you find it useful.
-
Sunday, June 28, 2009 5:30 PM
-
Tuesday, December 29, 2009 10:11 PMTamir:
Your code worked for one host but failed for another. I believe the problem is the second host doesn't allow reading rights in the inbox. They only allow a file to be created and it seems your code creates the file first, checks some stuff, and then writes the data. I need something that just creates the file with all the data in one step. I've been trying to understand how your code works and modify it but after a complete day of working on it, I'm just as clueless as I was when I started. Can you help? -
Sunday, January 03, 2010 7:25 PMTamir,
I like to evaluate how your library works. Unfortunately the URL http://www.codeproject.com/useritems/sharpSsh.asp is not working. Thanks.
fafaacom -
Monday, January 04, 2010 10:29 AMHi,
Can u send me some sample code to read and write image from the server -
Monday, January 04, 2010 10:31 AMhi,
can u send me the code.
anu1983in@gmail.com
Thanks,
Anu -
Friday, April 23, 2010 12:37 AM
Scott,
I just launched a .NET SFTP Library . Please try it out and let me know what you think.
Thanks,
Greg Finzer
Kellerman Software -
Tuesday, June 01, 2010 2:15 AM
Hi,
Please have a look at this .NET API which supports SFTP protocol :
Best Regards,
JSCAPE - Managed File Transfer & Security Solutions http://www.jscape.com -
Thursday, July 01, 2010 4:50 PM
Hello,
There are some commercial and free tools. Some commercial ones are at this SFTP libs web page , this blog post . It's from a commercial component software vendor . If someone loves FREE tool, try this SFTP library on codeproject.
- Edited by Clark MCPD Thursday, October 21, 2010 7:25 AM
-
Saturday, July 10, 2010 6:55 AM
It is hard to believe that microsoft does not have SFTP built in.
Here is what I did as an interim solution
Make sure you reference at the top of you VB script
Imports System.IO
I use Putty's psftp which is a stand alone program
Program found here http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
I then write a BATCH file to the users temp files system and execute the batch script all in my vb code as follows...
Hope this helps someone! Only way around not having to load anything and have to pay big $$$ to SFTP a file, however the script is dependent on having Putty's psftp file in place.
Imports System.IO Public Class Form1 Sub GetFile() Dim ftpscript As Integer = FreeFile() Dim tempscriptfile As String = My.Computer.FileSystem.GetTempFileName Dim tempbatfile As String Dim HostName As String = "username@hostname or IP address of SFTP server example: user@123.123.123.123" Dim password As String = "password for your user" FileOpen(ftpscript, tempscriptfile, OpenMode.Output) ' just use the same directory where you want to save your file for the lcd PrintLine(ftpscript, "lcd ""c:\path\to\local\directory""") PrintLine(ftpscript, "cd /remote/directory/") PrintLine(ftpscript, "get filetoget.txt") PrintLine(ftpscript, "bye") FileClose(ftpscript) tempbatfile = My.Computer.FileSystem.GetTempFileName tempbatfile = Mid(tempbatfile, 1, Len(tempbatfile) - 4) & ".bat" FileOpen(ftpscript, tempbatfile, OpenMode.Output) ' Enter the drive where your file will be loaded on the local drive. PrintLine(ftpscript, "C:") ' Change to where you want to save the file on the local drive make sure it is writable by the end user PrintLine(ftpscript, "CD ""c:\path\to\local\directory""")<br/> PrintLine(ftpscript, """C:\path\to\your\psftp\program\psftp.exe"" " & HostName & " -pw " & password & " -b " & """" & tempscriptfile & """") FileClose(ftpscript) Shell(tempbatfile, AppWinStyle.MinimizedNoFocus, True) Kill(tempbatfile) Kill(tempscriptfile) End Sub End Class
-
Thursday, July 22, 2010 6:26 PMIt really makes no sense that MS has not added this to .NET! This has been an issue for over 3 years!!!! Buy a company or something. But PLEASE add it.
-
Wednesday, October 13, 2010 4:53 AMIf you need SFTP functionality, you may want to use the Ultimate SFTP component for .NET . Sample code can also be found on its blog: SFTP blog .
-
Monday, November 15, 2010 7:17 AM
Hi,
I want to know any commercial library I can use to SCP between two remote hosts. Please let me know.
Regards,
Sundar
- Proposed As Answer by StevenRapp ATP Sunday, December 26, 2010 7:40 PM
-
Wednesday, December 15, 2010 10:37 PM
....Or maybe they can come out with PowerShell for Linux/Unix :-)
Then you would use WinRM
-Nikos
Νικοσ Γιαννιοσ -
Sunday, December 26, 2010 7:43 PM
Hi,
I want to know any commercial library I can use to SCP between two remote hosts. Please let me know.
Regards,
Sundar
You can setup two SCP file systems and transfer files easily as illustrated in the following example (copied from http://www.atp-inc.net/doc/sftp/Uploading_a_file_from_other_file_system.html ):
// Connect to an FTP file system.
Ftp ftpsys = new Ftp();
ftpsys.Connect( "192.168.126.128" , 21);
ftpsys.Authenticate( "test" , "test" );
// Connect to an SFTP file system.
Sftp sftp = new Sftp();
sftp.Connect( "192.168.126.128" , 2222);
sftp.Authenticate( "test" , "test" );
// Copy 'blog.txt' file from the FTP file system to the SFTP file system.
sftp.CopyFrom(ftpsys, "blog.txt" , "my blog on SFTP file system.txt" );
ftpsys.Disconnect();
sftp.Disconnect();
The example deals with FTP and SFTP, you can change it to two SCP file systems. It will work just fine.
-
Monday, January 03, 2011 7:59 AMHi Tamir,
I am using the open source provided at code project for SFTP. However it gives following error :
'Tamir.SharpSsh.jsch.Util' is inaccessible due to its protection level
Is Anyone aware of this error? How to resolve. Please help -
Friday, January 21, 2011 11:58 AM
Out of the solutions mentioned here, SharpSSH worked best for me. Download the binaries instead of trying to add the source project to your solution. When I did that, it did not work consistently for me. But the binaries work great. Here's how I use them:
public static void UploadToFTPSite(
string FilePath,
string Host,
string User,
string Password)
{
try
{
System.IO.FileInfo fileInf = new System.IO.FileInfo(FilePath);
SshConnectionInfo input = new SshConnectionInfo();
input.Host = Host;
input.User = User;
input.Pass = Password;
SshTransferProtocolBase sshCp = new Sftp(input.Host, input.User);
sshCp.Password = input.Pass;
sshCp.Connect();
string localFile = FilePath;
string remoteFile = fileInf.Name;
sshCp.Put(localFile, remoteFile);
sshCp.Close();
}
catch (Exception ex)
{
#if DEBUG
System.Diagnostics.Debugger.Break();
#endif
throw ex;
}
}Carlos A Merighe.
-
Thursday, June 09, 2011 9:31 PM
Guys! What you are looking for called SSH.NET now. It's further development of Sharp.SSH library and is completely reworked now. Check this out.
-
Tuesday, August 09, 2011 5:16 AM
it has passed a long time....
try http://www.example-code.com/csharp/sftp_readTextFile.asp and here is the document http://www.chilkatsoft.com/refdoc/csSFtpRef.html
这家伙很懒,什么都没写 -
Friday, August 19, 2011 3:12 PM
I tried the Chilkatsoft tool for a first connect and list directory files. Its quite intuitve to program and offers a good price compared with other commercial solutions.
However, error handling is done by boolean return value and three LastError Properties that allow to get last error in three different formats. So I have to handle the return value and the errorcode for each call. Why don't they use exceptions???
-
Monday, August 22, 2011 7:06 AMgod knows...
这家伙很懒,什么都没写 -
Saturday, August 27, 2011 4:04 PM
I have started creating a class library for SFTP transfers which uses the command line version of WinSCP for doing the hard work. I have created a DLL that stores the WinSCP executables as embedded resources and copy's them to the temp folder on instantiation.
I too, have used the Chilkat library before but decided to use WinSCP as it's free. I have started creating the library in the same way as Chilkat have done by storing the last error message of the last command run in a read only property. I have also used there method of setting a KeepSession log property to true for storing the full session log which is acquired from WinSCP.
I was just curious to know why you would rather use exceptions as opposed to a true or false value and a check last error property? Is changing my code to use exceptions a better way to go? For anyone interested, here is a link to an article discussing the basics on how to upload and download a file using VB.NET and WinSCP as a class library: http://zeusarticles.com/transfer-files-using-sftp-protocol-and-vbnet-free. The code in this article is similar to what I am using in the library I'm creating.
The class currently has methods for:
- UploadFile
- DownloadFile
- WriteFileText
- WriteFileBytes
- DeleteFile
- SetFileGroupAndOwner
- SetFilePermissions
- CreateDirectory
- DeleteDirectoy
-
Monday, May 07, 2012 1:50 PMUltimate SFTP component setup package has SFTP Console and SCP Console examples. It's a commercial product.