LiveCode LessonsLiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCodeTSNetHow to use tsNet to connect to an FTP server over SSL (FTPS)

How to use tsNet to connect to an FTP server over SSL (FTPS)

Introduction

Many FTP servers support connecting via SSL (FTPS) as this means that any authentication details are sent over the Internet in an encrypted manner rather than being transmitted in plain text.  Using tsNet to connect via FTPS is just as simple as any other connection type.

This lesson shows how to retrieve a directory listing from an FTP server by connecting to it over SSL.

Lay out the Stack

Create a new stack then drag a button and two fields onto it.

Set the name of the fields to "Output" and "Server Response".

Save this stack.

Create the Script

Edit the script of the button that you placed on the stack by right clicking on the button and selecting "edit script".

Add the following code.

on mouseUp pMouseButton
   -- Store the FTP server details in some variables
   put "ftp://test.rebex.net" into tFtpServer
   put "/pub/example/" into tFtpDirectory
   
   -- The tSettings array can be used to provide additional configuration details, in this case
   -- the username and password to use when transferring the files via FTP
   put "demo" into tSettings["username"]
   put "password" into tSettings["password"]
   
   -- To force FTP SSL mode, we use the following additional setting
   put true into tSettings["use_ssl"]
   
   -- Let the user know that the transfer is starting
   put "Downloading FTPS directory listing for" && tFtpDirectory && "from" && tFtpServer & cr into field "Output"
   
   -- Perform the upload
   put tsNetGetSync(tFtpServer & tFtpDirectory, tHeaders, tRecvHeaders, tResult, tBytes, tSettings) into tOutput
   
   -- Output the response to the user
   put "Transfer complete with server response code" && tResult after field "Output"
   put tOutput into field "Server Response"
end mouseUp
Click to copy

Test

Switch to Run mode and click the button.

More information

As can be seen in the code above, there is only one line of code needed to ensure that the FTP connection is performed over SSL.

In this instance, setting tSettings["use_ssl"] to true is all that is needed.  This forces the connection to use what is called "explicit FTPS" which is the most common form of FTP encryption.  Note that the connection string contained in tFtpServer still starts with "ftp://".

This additional line is not needed if the server you are connecting to only supports the deprecated "implicit FTPS" connection method.  To connect via implicit FTPS, use "ftps://" intead of "ftp://" in your connection string.

 

7 Comments

Mike Felker

The demo fails. I cannot get this working with my own server credentials no matter what I do. The port is wrong as well. Comes up as 21 - should be 22 for ssh and scp (although we will change it once everything is working)

Please advise.

Mike

Mike Felker

This is the error I receive with just the demo code: Downloading FTPS directory listing for /pub/example/ from ftp://test.rebex.net
Transfer complete with server response code tsneterr: (64) Requested SSL level failed

Please advise.

Mike

Matthias Rebbe

Mike,
i tried the above script now with 2 different FTP servers which support explicit FTPs. In both cases the script worked without a problem.

TsNet returns the Curl error codes. According to the Curl documentation error 64 means that an SSL connection could not be established. This is normally the case when the client tries to connect to the server using an SSL level the server does not support.

You are writing about port 22, but port 22 is used for SFTP.
So by any chance is it possible that you are trying to use the above script to connect to an SFTP Server?

Matthias

Charles Warwick

Hi Mike,

As Matthias has mentioned above, this script is particularly for FTP servers using SSL/TLS. SSH and SCP are different protocols.

The lesson titled, "How to asynchronously download via SFTP directly to a file" provides an example of how to use tsNet with SFTP.

Regards,

Charles.

Mike Felker

Turns out I simply did not have the business license. Now that we do, it works, but I am having a problem downloading multiple files in the background from a loop. I CANNOT use outside scripting languages, like JSON data or PHP. I have to do this 100% inside of a Livecode program. If I use answer boxes, it downloads all files correctly, but without them, it fails. I have a feeling the downloads are happening too fast and so some create errors and do not download. Somehow, I need the program to know when the file is done downloading and then starts the next one. The built in ftp livecode commands (libURLDownloadToFile) is perfect but not secure. I need the functionality of libURLDownloadToFile but using SSH, SCP or SFTP.

Thank you for your help.

Mike

Matthias Rebbe

I´ve already answered your similar question in an other lesson. See it here

http://lessons.livecode.com/m/4071/l/945907-how-to-download-multiple-files-in-the-background-with-tsnet

David Resseguie

Dang, was this ever extremely helpful! My ISP moved our site to a new server with sftp only functionality. This helped me rewrite my code, and pointed to way to the functionality I needed! Thanks.

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.