2012-07-31 86 views
0

我有要求从一台远程服务器下载大文件(20GB +)到另一台C#远程服务器。我们已经为其他文件操作建立了ssh连接。我们需要以二进制模式从ssh会话启动ftp下载(这是远程服务器要求以特定格式传送文件)。问题 - 如何在ssh会话中发送ftp命令来建立与证书的连接并将模式设置为'bin'?基本上相当于以下使用ssh的:C# - 使用ftp通过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(); 

回答

1

SSH是一个“安全壳”这就好比一个命令提示符中,你给它的命令来运行。它不是FTP。 SSH可以执行FTP实用程序或应用程序。

SSH确实有一些称为SFTP(或SSH文件传输协议)的东西,但是没有内置于.NET(即BCL)的内容来做到这一点。 FtpWebRequest不支持SFTP。

尽管共享“FTP”,它们是不同的协议,并且彼此不兼容。

如果您想启动FTP下载,您必须告诉SSH执行某种实用程序。如果您想启动SFTP传输,则可以发出sftp命令。但是,另一端需要支持sftp。或者,您可以发出scp命令(安全副本);但另一方面,另一端将需要支持该协议(它与SFTP和FTP不同)...

如果你想写另一端,你必须找到一个第三方库,它执行SFTP或SCP ...

+0

感谢您的回复。正如我所提到的,我们已经在使用SSH执行很多文件操作了(从c#服务连接到我们的unix框)。仅当我们使用特定凭证进行ftp连接时,才会以所需格式传送来自远程服务器的文件。它不支持sftp。最有可能的是,我打算执行批处理文件以使用ftp从ssh命令下载文件(ssh连接从web服务启动)。 – user1408552 2012-08-01 12:13:47

0

我最喜欢的库,以实现你所需要的是从奇尔卡特软件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; 
} 
相关问题