简体   繁体   English

C#-使用ssh上的ftp将文件从一台远程服务器下载到另一台

[英]C# - Download file using ftp over ssh from one remote server to another

I have requirement to download large files (20GB+) from one remote server to another remote server in C#. 我需要使用C#将大文件(20GB +)从一台远程服务器下载到另一台远程服务器。 We already make ssh connections for other file operations. 我们已经为其他文件操作建立了ssh连接。 We need to initiate ftp download from ssh session in binary mode (this is remote server requirement to deliver file in specific format). 我们需要以二进制模式从ssh会话启动ftp下载(这是远程服务器要求以特定格式传送文件)。 Question - How do I send ftp command in ssh session to make connection with credentials and set mode as 'bin'? 问题-如何在ssh会话中发送ftp命令以与凭据建立连接并将模式设置为“ bin”? Basically equivalent of following using ssh: 基本上等同于使用ssh的以下操作:

FtpWebRequest request =(FtpWebRequest)WebRequest.Create(
        @"ftp://xxx.xxx.xxx.xxx/someDirectory/someFile");

request.Credentials = new NetworkCredential("username", "password");
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

SSH is a "Secure SHell" This is like a cmd prompt in that you give it commands to run. SSH是“安全外壳”,就像cmd提示符一样,您可以给它运行命令。 It is not FTP. 它不是FTP。 SSH could execute an FTP utility or application. SSH可以执行FTP实用程序或应用程序。

SSH does have something called SFTP (or SSH File Transfer Protcol) but there is nothing built into .NET (ie the BCL) to do that. SSH确实有一个叫做SFTP(或SSH文件传输协议)的东西,但是.NET(即BCL)没有内置功能可以做到这一点。 FtpWebRequest does not support SFTP. FtpWebRequest不支持SFTP。

Despite sharing "FTP" they're different protocols and are not compatible with each other. 尽管共享“ FTP”,但它们是不同的协议,并且彼此不兼容。

If you want to initiate an FTP download, you'll have to tell SSH to execute a utility of some sort. 如果要启动FTP下载,则必须告诉SSH执行某种实用程序。 If you want to initiate an SFTP transfer you can issue the sftp command. 如果要启动SFTP传输,则可以发出sftp命令。 But, the other end will need to support sftp. 但是,另一端将需要支持sftp。 Alternatively you an issue the scp command (secure copy); 或者,您发出scp命令(安全副本); but again, the other end will need to support that protocol (it's different from SFTP and FTP)... 但同样,另一端将需要支持该协议(不同于SFTP和FTP)...

If you want to write the other end, you'll have to find a third-party library that does SFTP or SCP... 如果要编写另一端,则必须找到执行SFTP或SCP的第三方库。

My favorite lib to achieve what you need is Chilkat from ChilkatSoft (Disclaimer: I am just a paying user with no affiliation to the company). 我最喜欢的库可以满足您的需求,它是ChilkatSoft的Chilkat (免责声明:我只是一个付费用户,与该公司没有任何隶属关系)。

Chilkat.SFtp sftp = new Chilkat.SFtp();

//  Any string automatically begins a fully-functional 30-day trial.
bool success;
success = sftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

//  Set some timeouts, in milliseconds:
sftp.ConnectTimeoutMs = 5000;
sftp.IdleTimeoutMs = 10000;

//  Connect to the SSH server.
//  The standard SSH port = 22
//  The hostname may be a hostname or IP address.
int port;
string hostname;
hostname = "www.my-ssh-server.com";
port = 22;
success = sftp.Connect(hostname,port);
if (success != true) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

//  Authenticate with the SSH server.  Chilkat SFTP supports
//  both password-based authenication as well as public-key
//  authentication.  This example uses password authenication.
success = sftp.AuthenticatePw("myLogin","myPassword");
if (success != true) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

//  After authenticating, the SFTP subsystem must be initialized:
success = sftp.InitializeSftp();
if (success != true) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

//  Open a file on the server:
string handle;
handle = sftp.OpenFile("hamlet.xml","readOnly","openExisting");
if (handle == null ) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

//  Download the file:
success = sftp.DownloadFile(handle,"c:/temp/hamlet.xml");
if (success != true) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

//  Close the file.
success = sftp.CloseHandle(handle);
if (success != true) {
    MessageBox.Show(sftp.LastErrorText);
    return;
}

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

相关问题 从被动服务器C#的原始FTP下载文件 - Raw FTP Download File from Passive Server C# 在 C#/.NET 中向/从 FTP 服务器上传和下载文件 - Upload and download a file to/from FTP server in C#/.NET 如何使用 SharpSsh 和 C# 将文件从一个文件夹移动到远程服务器上的另一个文件夹 - How to move a file from one folder to another folder on a remote server using SharpSsh and C# 使用Filezilla服务器在C#中通过SSL通过ftp发送文件 - sending file using ftp over ssl in C#, with filezilla server 使用 C# 以多线程的形式从 FTP 服务器下载单个文件 - Download single file from FTP server in chunks in multiple threads using C# 使用 C# 通过 http 下载 ftp 文件? - Download ftp files over http using C#? 使用C#从FTP服务器读取CSV文件 - Read a CSV file from FTP Server Using C# 如何使用C#中的WinSCP .NET程序集将内容从一个远程目录复制到同一服务器中的另一个目录 - How to copy things from one remote directory to another in the same server using WinSCP .NET assembly in C# 使用C#从FTP服务器下载名称包含特殊字符的文件 - Download a file with name including special characters from FTP server in C# 错误 550:使用 C# 从 ftp 服务器下载文件时,指定的网络名称不再可用 - Error 550: the specified network name is no longer available when download file from ftp server with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM