Raphael Melo De Lima
Raphael Melo De Lima

Reputation: 215

Download file using ssh with simple-ssh npm

I need to download a file on the server using ssh from remote to local. I'm using the simple-ssh module from npm link to npm module

I can update a file that is already on the server, however I need to download such a file

var SSH = require('simple-ssh');
let ssh = new SSH({
                host: 'remote_server_ip',
                user: 'my_user',
                pass: 'my_pass'
            });
            
            ssh.exec(`cat > ${filePath}`, {
                in: fs.readFileSync('/home/raphael/Documentos/teste.bin')
            }).start();
            
            //my filePath /arq_soa/arquivos_validador/Envio/tst_acesso

Can someone help me?, i'm lost

After help of @Carlos Jafet Neto, my code has changed, now:

var Client = require('ssh2-sftp-client');
let sftp = new Client
            sftp.connect({
                host: 'remote_server_iṕ',
                port: 22,
                username: 'username',
                password: 'password'
            }).then(() => {
                return sftp.list(`${pathArquivoValidador}`);
            }).then(async (data) => {
                let len = data.length;
                await data.forEach(x => {
                    let remoteFilePath = `${pathArquivoValidador}` + params.nmArquivo;                    
                    sftp.get(remoteFilePath).then((stream) => {
                        let file = './home/raphael/Documentos/' + params.nmArquivo;
                        fs.writeFile(file, stream, (err) => {
                            if (err) console.log(err);
                        });
                    });
                    // console.log(x);
                });
            }).catch((err) => {
                console.log(err, 'catch error');
            });

but the sftp.get are bringing me the following error

{ [Error: ENOENT: no such file or directory, open './home/raphael/Documentos/tst_acesso'] errno: -2, code: 'ENOENT', syscall: 'open', path: './home/raphael/Documentos/tst_acesso' }

Upvotes: 1

Views: 3370

Answers (1)

Carlos Jafet Neto
Carlos Jafet Neto

Reputation: 891

Try to get first only the files that are in your folders without using variables, using your path as a string to eliminate errors.

var Client = require('ssh2-sftp-client');
let sftp = new Client
            sftp.connect({
                host: 'remote_server_iṕ',
                port: 22,
                username: 'username',
                password: 'password'
            }).then(() => {
                return sftp.list('/');
            }).then(async (files) => {
                console.log(files);
                len = files.length;
                await files.forEach(x => {
                    let remoteFilePath = '/' + x.name;
                    sftp.get(remoteFilePath).then((stream) => {
                        let file = './ftp/' + x.name;
                        fs.writeFile(file, stream, (err) => {
                            if (err) console.log(err);
                        });
                    });
                });
            }).catch((err) => {
                console.log(err, 'catch error');
            });

To get a single file:

var Client = require('ssh2-sftp-client');
let sftp = new Client
            sftp.connect({
                host: 'remote_server_iṕ',
                port: 22,
                username: 'username',
                password: 'password'
            }).then(() => {
                let remoteFilePath = '/' + fileName;
                sftp.get(remoteFilePath).then((stream) => {
                    let file = './ftp/' + x.name;
                    fs.writeFile(file, stream, (err) => {
                        if (err) console.log(err);
                    });
                    sftp.end();
                });
            }).catch((err) => {
                console.log(err, 'catch error');
            });

Use this to close the ftp connection:

sftp.end();

Upvotes: 2

Related Questions