简体   繁体   English

如何与SFTP服务器通信

[英]How to communicate with SFTP server

I've written a service for our customer that automatically transmits files to given destinations using FTP.我为我们的客户编写了一项服务,该服务使用 FTP 自动将文件传输到给定的目的地。 For historic reasons I'm using WinInet to perform the FTPing.由于历史原因,我使用 WinInet 来执行 FTPing。 All works well, but now the customer wants to add one destination that only accepts SFTP connections.一切正常,但现在客户想要添加一个仅接受 SFTP 连接的目的地。

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?我真的不喜欢从头开始实现这个想法,所以有没有办法在本地或通过 WinInet 与 SFTP 服务器进行通信? Are there any system libraries that I can use (I have no fear of P/Invoke :) )?有没有我可以使用的系统库(我不怕 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.在任何版本的 .NET 框架中都不支持 SFTP。


You have to use a third party library for SFTP.您必须为 SFTP 使用第三方库。

You can use WinSCP .NET assembly .您可以使用WinSCP .NET 程序集 There's even a WinSCP NuGet package .甚至还有一个WinSCP NuGet 包

A trivial SFTP upload C# example:一个简单的 SFTP 上传 C# 示例:

// 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.您可以让WinSCP GUI为您生成一个 SFTP 代码模板,如上所示,包括 C#、VB.NET 和 PowerShell。

在此处输入图片说明


The assembly is just a wrapper around WinSCP scripting, so it's not a completely native .NET code.该程序集只是 WinSCP 脚本的包装器,因此它不是完全本机的 .NET 代码。 As such it does not fit all use cases in .NET framework.因此,它并不适合 .NET 框架中的所有用例。 It is mostly suitable for automating tasks, somewhat for developing GUI applications, and not really for web applications.它主要适用于自动化任务,有点适用于开发 GUI 应用程序,而不是真正适用于 Web 应用程序。

For a fully native .NET SFTP library, see SSH.NET , which is strangely not mentioned in any answer yet.对于完全本机的 .NET SFTP 库,请参阅SSH.NET ,奇怪的是在任何答案中都没有提到它。

(I'm the author of WinSCP) (我是 WinSCP 的作者)


Windows 10 also come with command-line OpenSSH sftp client . Windows 10 还带有命令行 OpenSSH sftp客户端 It can also be downloaded for older versions of Windows.也可以为旧版本的 Windows 下载它。

Unfortunately, SFTP is not natively supported by WinInet or any other standard Windows libraries.不幸的是,WinInet 或任何其他标准 Windows 库本身并不支持 SFTP。

I've had good luck with /n Software's IP*Works SSH for .NET in talking to a large variety of SFTP servers.我很幸运使用 /n Software 的IP*Works SSH for .NET与各种 SFTP 服务器通信。

I created a demo of interactions with SFTP server using WinSCP.我创建了一个使用 WinSCP 与 SFTP 服务器交互的演示。

This application is able to upload, delete, rename and fetch info of files system in an SFTP Server using WinSCP .NET Assembly.此应用程序能够使用 WinSCP .NET 程序集上传、删除、重命名和获取 SFTP 服务器中文件系统的信息。

Take a look at: https://github.com/ducfilan/SFTP-with-WinSCP看一看: https : //github.com/ducfilan/SFTP-with-WinSCP

http://sourceforge.net/projects/sharpssh/ could be your best best. http://sourceforge.net/projects/sharpssh/可能是您最好的选择。 I ended up writing scripts for WinSCP though, which was easier...不过,我最终还是为 WinSCP 编写了脚本,这更容易……

EDIT for clarity, I'd recommend something like https://github.com/sshnet/SSH.NET now as SharpSSH is a dead project为了清楚起见编辑,我现在推荐像https://github.com/sshnet/SSH.NET这样的东西,因为 SharpSSH 是一个死项目

the .NET Framwork supports FTP via the FtpWebRequest since version 2.0. .NET 框架从 2.0 版开始通过 FtpWebRequest 支持 FTP。 No support for SFTP yet.尚不支持 SFTP。

If you need both FTP and SFTP you have to try a third party component.如果您同时需要 FTP 和 SFTP,则必须尝试第三方组件。 There is an open source implementation of SFTP from Tamir Gal which is often recommended. Tamir Gal 有一个开源的 SFTP 实现,经常被推荐。 Or you can try one of commercial SFTP components such as our Rebex SFTP .或者,您可以尝试使用一种商业 SFTP 组件,例如我们的Rebex SFTP

Following code is taken from Rebex SFTP tutorial page :以下代码取自Rebex SFTP 教程页面

// 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.如果您不确定是否需要 FTPS 或 SFTP,您可以查看包含两者的Rebex 文件传输包 The FTP API is almost identical to the SFTP one. FTP API 几乎与 SFTP API 相同。

JFYI: .NET 中 SFTP 功能最丰富的组件可能是我们的SFTPBlackbox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM