I've written a service for our customer that automatically transmits files to given destinations using FTP. For historic reasons I'm using WinInet to perform the FTPing. All works well, but now the customer wants to add one destination that only accepts SFTP connections.
I do not really like the idea of implementing this from scratch, so is there a way to communicate natively or through WinInet with a SFTP server? Are there any system libraries that I can use (I have no fear of P/Invoke :) )? Do I have to purchase third party components for that - if so, which ones would you suggest?
There's no support for SFTP in .NET framework, in any version.
You have to use a third party library for SFTP.
You can use WinSCP .NET assembly. There's even a WinSCP NuGet package.
A trivial SFTP upload C# example:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
session.PutFiles(#"d:\toupload\*", "/home/user/").Check();
}
There are lot of other examples.
You can have WinSCP GUI generate an SFTP code template, like above, for you, including C#, VB.NET and PowerShell.
The assembly is just a wrapper around WinSCP scripting, so it's not a completely native .NET code. As such it does not fit all use cases in .NET framework. It is mostly suitable for automating tasks, somewhat for developing GUI applications, and not really for web applications.
For a fully native .NET SFTP library, see SSH.NET, which is strangely not mentioned in any answer yet.
(I'm the author of WinSCP)
Windows 10 also come with command-line OpenSSH sftp client. It can also be downloaded for older versions of Windows.
Unfortunately, SFTP is not natively supported by WinInet or any other standard Windows libraries.
I've had good luck with /n Software's IP*Works SSH for .NET in talking to a large variety of SFTP servers.
http://sourceforge.net/projects/sharpssh/ could be your best best. I ended up writing scripts for WinSCP though, which was easier...
EDIT for clarity, I'd recommend something like https://github.com/sshnet/SSH.NET now as SharpSSH is a dead project
I created a demo of interactions with SFTP server using WinSCP.
This application is able to upload, delete, rename and fetch info of files system in an SFTP Server using WinSCP .NET Assembly.
Take a look at:
https://github.com/ducfilan/SFTP-with-WinSCP
the .NET Framwork supports FTP via the FtpWebRequest since version 2.0. No support for SFTP yet.
If you need both FTP and SFTP you have to try a third party component. There is an open source implementation of SFTP from Tamir Gal which is often recommended. Or you can try one of commercial SFTP components such as our Rebex SFTP.
Following code is taken from Rebex SFTP tutorial page:
// create client, connect and log in
Sftp client = new Sftp();
client.Connect(hostname);
client.Login(username, password);
// upload the content of 'c:\data' directory and all subdirectories
// to the '/wwwroot' directory at the server
client.PutFiles(#"c:\data\*", "/wwwroot", SftpBatchTransferOptions.Recursive);
client.Disconnect();
If you are not sure whether you will need FTPS or SFTP you can check the Rebex File Transfer Pack which includes both. The FTP API is almost identical to the SFTP one.
JFYI: probably the most feature-rich components for SFTP in .NET is our SFTPBlackbox.
I use edtFTPnet/PRO
http://www.enterprisedt.com/products/edtftpnetpro/overview.html
works great
Related
I have been making a simple ftp of my JCL after sending site filetype=jes with my C# code.
However now I am required to use SFTP to send and receive files.
I found renci.SshNet but it does not have a method to send command.
Can someone please tell me how I can SFTP a JCL to mainframe using C#?
What is the best method to go about this?
SFTP (not to be confused with FTPS) is not supported by .NET out of the box. You have to rely on a 3rd party library. See related question here: SFTP Libraries for .NET
The answer for your stated problem is that you need FTPS and not SFTP to support JES extensions for job submission and retrieval (as well as other z/OS specific capabilities).
Here is an article that goes over the details of FTPS in c#. Its the same command sequence using the SITE command as you used under FTP.
For a comparison of SFTP and FTPS here is some information.
Found this link about FTPS setup on z/OS from Share. This should get you started. It seems more practical and applied than going to knowledge center and shows the various options and capabilities. The original author, Alfred B Christensen, is an IBMer. The material is from 2011 but is still mostly applicable today.
Here is a comparison of the technologies:
Here is some more background.
The issue is that ...sftp understands a set of commands subcommands similar to those of ftp. according to the linked IBM manual for SFTP. The sftp server implementation provided by IBM does not have the full set of extensions provided by the FTP Server
There are other servers out there with associated clients that can provide the capability. I'm not endorsing this product but it seems to be popular from google searches I've done Tectia The default implementation of sftp server from IBM does not support the same extensions that ftp does.
If you're looking for a seamless transition and need transport security ftps is a better option but likely requires configuration on the z/OS system if its not already enabled.
The issue is not simply the client, it is the capability of the sftp implementation on z/OS as well.
The native SFTP from IBM does not support all the features of FTP. You will need to use Co:Z toolkit SFTP from Dovetailed. It's free to use with paid support available.
I need to create a C# client with .NET Framework 4.6.2 to connect a server.
My client offers me the possibility to connect to a SFTP server or to a FTPS server, but I don't know which one is the best to connect with.
On this page, I have found this:
No built-in SSH/SFTP support in VCL and .NET frameworks
I need to connect to a server to upload and download files. I also need to monitor a directory on the server to know when a file is on that remote directory.
Searching on Internet I'm not sure if the .NET libraries (SSHNet) that implements SFTP protocol are good enough to a production environment.
I think SFTP is the best option to use but FTPS could be easier to implement a C# client for it.
Or maybe I can use libssh2 to implement a C program that do the job to monitor a remote directory, download any new file on it and upload the files that I need to upload.
Any advice?
If both protocols are fine for your actual needs (as suggested in your question and comments), if it's only a matter of "what's the easiest to use in .Net", I would simply go for FTPS.
It's very fast to implement, since you'll find all what you need in the framework ("FtpWebRequest" class, or more recently "WebClient", etc), even on old versions of the framework.
You can find plenty resource about this on the web or on SO
You have mentioned that you need to "monitor" a folder on remote server. Of course there's no problem with FTPS to retrieve the list of all files of a folder, but it will be in "pull" mode, as frequently as you wish. There's no way for the server itself to push you a notification every time a new file has been dropped. So if you need some real-time notifications, it's not optimal.
I'm creating an application to synchronize a local folder on my computer with a FTP server.
Since I don't wan't to access the FTP server directly over it's address, because it would be really complicated, I wan't to map the server temporary to a network folder so that my synchronization algorithm can work on it like on a local folder.
Is there something like this:
//Map the server to the specified path
FtpMapper.MapFtpServer("ftp://myserver/", "username", "password", "M:\\");
DirectoryInfo info = new DirectoryInfo("M:\\");
/* Do some work on the directory */
FtpMapper.UnmapFtpServer();
There isn't such a library, as far as I know.
This requires a windows filesystem driver that understands FTP commands and a .NET wrapper over it.
I have seen such drivers (without a wrapper) as part of commercial FTP suites, but nothing open source or free.
NetDrive boasts offering an SDK that lets you use it (netdrive) from your application. This is your only option if you don't want to write FTP code (or use third-party FTP component).
A programming solution to your task would be to take our Callback File System (to create a virtual drive) and our FTPSBlackbox (FTP/FTPS components for .NET) and combine them in your application.
I'm surprised by the lack of products out there for running your own FTP server in managed code. I'm looking for either a product or thorough source code for building an FTP server in C# that supports SSL. I need complete control over the directory/file handling, not just serving files from a local directory.
Clever Internet .NET Suite looks to be exactly what I'm looking for. However, when I tried it out I got errors connecting to the FtpServerSSL sample from FileZilla when SSL was enabled. It worked perfectly without SSL, so I wrote to their support, but I haven't heard a response from them yet.
Never done it myself, but you could try one of these:
http://www.c-sharpcorner.com/UploadFile/psingh/FTPServerinCSharp11162005015958AM/FTPServerinCSharp.aspx
http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7409/
You secure the port using httpcfg or netsh in windows, not in the app.
We plan to release FTP / FTPS server components within a month as part of our SecureBlackbox product. The components are currently being internally tested. As SecureBlackbox is a security suite, SSL part will work there :).
My Windows client application (written in C#) works with the remote MySQL database deployed on *NIX server.
I'm going to add the ability to store files on the server. I was never fond of storing files in DB, so the files will be stored in the file system. The application will need to upload, download, and delete files from server.
Any approach I can think of requires additional service to be deployed on server.
So far my favorite is SFTP*, but there are other solutions which are IMO inferior:
Web Service - are there any? haven't done any research in this direction.
FTP server - brings at least all the problems connected with protocol itself
WebDAV - interesting option, considering there is Apache module and .NET libraries
Some ad-hoc script (clearly the worst solution)
*I intend to use SharpSSH library, and public key authentication to access special account on the server which will be restricted to SSH operations only
Edit: corrected some terms confusion, thanks to #vartec
SCP has rather limited functionality. SFTP and FISH give you a lot more then plain SCP. (note, that SFTP should not be confused with FTPS)
Example of SFTP C# implementation.
Advantage over other options — no extra configuration at server side required.
I would use vsftpd on the *NIX server side: it's small and easy to configure, and I haven't had any problems with it since I initially set it up about 6 months ago so it's also reliable. I just use regular FTP, but you can do SSL with vsftpd if you're concerned about security.
On the client side there is this free, open source, FTP Client Library for C# made by Dan at C-SharpCorner.com. It works, and it's easy to extend its functionality to do more things. For example, I added a function for changing file permissions on the remote server.
putty and winscp are all you need.