FTP test with Node and JavaScript

Script to verify that the FTP connection to the server is available and that remote commands can be invoked.

FTP checking is suitable for a Node project that requires the use of FTP to exchange files between the local computer and the server, on a mutual or dedicated hosting.

The script uses the jsftp module and does not work asynchronously. First, the ability to connect to the stat command is checked. It returns an error if the server cannot connect or rejects it.

If the connection is established, another command is tried, the date of the last change of the file is read, the name and path to which is indicated.

The script can be edited to test other commands, an exhaustive list of possible commands can be found in the jsftp.js file.

The script is executed by the following command:

node testftp.js paramètres 

The following parameters are required:

-l   suivi du login
-p   suivi du mot de pass
-f   suivie de l'URL dans le protocole FTP.
nom de fichier avec chemin complet à partir de la racine du dossier public.

Example

 node testftp.js -lnom -pxxxxxx -fftp.example.com www/monfichier.html

Script Source:

var JSFtp = require("jsftp")

function main(argc,argv)
{
    processCommand(argc, argv.slice(1))

    OPTIONS={"host":server,"port":21,"user":user,"pass":pass,"debugMode":true}

    var connection = new JSFtp(OPTIONS);
    console.log("Checking FTP on "+ server)      
    connection.raw.stat(function(err, data) {
        if(err) {
            console.log("Server not available through FTP. Error:" + err)
            return
        }
        console.log("Server OK.")

        console.log("Checking date of " + filename)   

        connection.raw.mdtm(filename, function(err, data) {
            if(err) {
                console.log("Date not returned. Error: " + err)
                return
            }    
            console.log("Date/Time: " + data.text.substr(4))
            console.log("Data returned:")
            console.log(JSON.stringify(data))
            connection.raw.quit(function(err, data) {
                console.log("Bye!")
                process.exit(1)
            })
        })
    })
}    

main(process.argv.length-1,process.argv.slice(1))

Download full script:

Do not forget to install jsftp before running the script (npm install jsftp).