35165

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

Question:

I have requirement to download large files (20GB+) from one remote server to another remote server in C#. We already make ssh connections for other file operations. We need to initiate ftp download from ssh session in binary mode (this is remote server requirement to deliver file in specific format). Question - How do I send ftp command in ssh session to make connection with credentials and set mode as 'bin'? Basically equivalent of following using 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();

Answer1:

SSH is a "Secure SHell" This is like a cmd prompt in that you give it commands to run. It is not FTP. SSH could execute an FTP utility or application.

SSH does have something called SFTP (or SSH File Transfer Protcol) but there is nothing built into .NET (i.e. the BCL) to do that. FtpWebRequest does not support SFTP.

Despite sharing "FTP" they're different protocols and are not compatible with each other.

If you want to initiate an FTP download, you'll have to tell SSH to execute a utility of some sort. If you want to initiate an SFTP transfer you can issue the sftp command. But, the other end will need to support sftp. Alternatively you an issue the scp command (secure copy); but again, the other end will need to support that protocol (it's different from SFTP and FTP)...

If you want to write the other end, you'll have to find a third-party library that does SFTP or SCP...

Answer2:

My favorite lib to achieve what you need is Chilkat from ChilkatSoft <em>(Disclaimer: I am just a paying user with no affiliation to the company).</em>

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; }

Recommend

  • Make synchronous Http/REST call in WP7?
  • SOAP web service with ksoap2 lib
  • Guzzle Error with Laravel. ClientException in RequestException.php line 107:
  • Cast uint -> double invalid?
  • TelephonyManager crashing on android studio
  • What to use (best/good practice) for the secret key in HMAC solution?
  • IIS7 Application Request Routing HTTPS
  • Geo Fix not working in Android SDK 2.2
  • `docker cp` doesn't copy file into container
  • mysql table locked after php crashes
  • How to check disabled jobs with Jenkins server?
  • View Paypal shopping cart contents on my site
  • Python Paramiko send CTRL+C to an ssh shell
  • Recreate the Oracle DUAL table
  • Timeout a query
  • How dotnet build chooses the output name
  • ASP.NET windows authentication should always ask for credentials
  • How to Save JSON data to SQL server database in C#?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • How can I get the full list of running processes on a Mac from a python app
  • VSCode change debug shell to bash on windows
  • Error processing multiple files
  • Check for zero lines output from command over SSH
  • How to Cache Real-time Data?
  • Spark fat jar to run multiple versions on YARN
  • Linq Objects Group By & Sum
  • Optimizing database types to compact database (SQLite)
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Buffer size for converting unsigned long to string
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?