node-ftps
FTP, FTPS and SFTP client for node.js, mainly a lftp
wrapper.
Requirements
You need to have the executable lftp
installed on your computer.
LFTP Homepage
Windows (Chocolatey)
C:\> choco install lftp
OSX (Homebrew)
sudo brew install lftp
Linux
sudo apt-get install lftp
sudo yum install lftp
Installation
npm install ftps
Usage
var FTPS = require('ftps');
var ftps = new FTPS({
host: 'domain.com',
username: 'Test',
password: 'Test',
protocol: 'sftp',
port: 22,
escape: true,
retries: 2,
timeout: 10,
retryInterval: 5,
retryMultiplier: 1,
requiresPassword: true,
autoConfirm: true,
cwd: '',
additionalLftpCommands: '',
requireSSHKey: true,
sshKeyPath: '/home1/phrasee/id_dsa',
sshKeyOptions: ''
});
ftps.cd('some_directory').addFile(__dirname + '/test.txt').exec(console.log);
Documentation
Here are some of the chainable functions :
ftps.ls()
ftps.pwd()
ftps.cd(directory)
ftps.cat(pathToRemoteFiles)
ftps.put(pathToLocalFile, [pathToRemoteFile])
ftps.get(pathToRemoteFile, [pathToLocalFile])
ftps.mv(from, to)
ftps.rm(file1, file2, ...)
ftps.mkdir(pathToNewDir, mode)
ftps.rmdir(directory1, directory2, ...)
ftps.mirror({
remoteDir: '.',
localDir: '.',
filter: /\.pdf$/,
parallel: true / Integer,
upload: true,
})
If you want to escape some arguments because you used "escape: false" in the options:
ftps.escapeshell('My folder');
Execute a command on the remote server:
ftps.raw('ls -l')
To see all available commands: LFTP Commands
For information, ls, pwd, ... rm are just some alias of raw() method.
Run the commands
ftps.exec(function (err, res) {
});
Also, take note that if a command fails it will not stop the next commands from executing, for example:
ftps.cd('non-existing-dir/').addFile('./test.txt').exec(console.log);
For using the stream, use the execAsStream()
command which returns a stream.
var ftps = new lftp(config)
var stream = ftps.raw('find').execAsStream()
stream.pipe(process.stdout)
Note on Using LFTP Commands
Normally in the lftp cli you would make a file of set
commands and pass that file name into lftp with the -c
option. However, ftps will do that for you with the additionalLftpCommands
option.
For instance, to connect to a legacy sftp server you can do:
const ftps = new FTPS({
additionalLftpCommands: 'set sftp:connect-program "ssh -a -x -o KexAlgorithms=diffie-hellman-group1-sha1"',
requireSSHKey: false,
});
This is also instead of making a ~/.lftprc
file. As you can see, you just put anything that would go into the command file into the option separated by a ;
.
Additional command can be found here or by running man lftp
.
PS: Any pull requests are welcome :-)