【问题标题】:C# - Download file using ftp over ssh from one remote server to anotherC# - 使用 ftp 通过 ssh 从一台远程服务器下载文件到另一台
【发布时间】:2012-07-31 16:54:10
【问题描述】:

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

【问题讨论】:

    标签: c# ssh ftp


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 感谢您的回复。正如我所提到的,我们已经使用 SSH 执行了很多文件操作(从 c# 服务连接到我们的 unix 机器)。仅当我们使用特定凭据进行 ftp 连接时,来自远程服务器的文件才会以所需格式传送。它不支持 sftp。很可能我打算使用 ftp 从 ssh 命令执行批处理文件以下载文件(ssh 连接是从 Web 服务启动的)。
    【解决方案2】:

    我最喜欢的 lib 是来自 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 2013-12-03
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      相关资源
      最近更新 更多