[英]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.