Download file using ssh with simple-ssh npmHow do I download directory with all files and folders inside using npm module ssh2 for nodejs?How to use SSH to run a shell script on a remote machine?Download a file with Android, and showing the progress in a ProgressDialogUsing node.js as a simple web serverHow can I update NodeJS and NPM to the next versions?ssh “permissions are too open” errorHow to download a file from server using SSH?Find the version of an installed npm packagenpm throws error without sudoWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?What is the --save option for npm install?

Why/when is AC-DC-AC conversion superior to direct AC-Ac conversion?

Can two figures have the same area, perimeter, and same number of segments have different shape?

How to avoid unconsciously copying the style of my favorite writer?

What does "see" in "the Holy See" mean?

Is it legal for private citizens to "impound" e-scooters?

Why are so many countries still in the Commonwealth?

How do we explain the E major chord in this progression?

How to Create an Image for Cantor's *Diagonal Argument* with a Diagonal Oval

Are there any examples of technologies have been lost over time?

Why didn't Britain or any other European power colonise Abyssinia/Ethiopia before 1936?

Convert every file from JPEG to GIF in terminal

Will any serial mouse connect to Classic Macs?

Commercial jet accompanied by small plane near Seattle

How to judge a Ph.D. applicant that arrives "out of thin air"

How much were the LMs maneuvered to their landing points?

High income, sudden windfall

How do I run a game when my PCs have different approaches to combat?

Calling a Macro as Argument of Another Macro

The Sword in the Stone

Piece-drop Mate #2

What do I do when a student working in my lab "ghosts" me?

Send a single HTML email from Thunderbird, overriding the default "plain text" setting

Is there a reason why I should not use the HaveIBeenPwned API to warn users about exposed passwords?

Problem in styling a monochrome plot



Download file using ssh with simple-ssh npm


How do I download directory with all files and folders inside using npm module ssh2 for nodejs?How to use SSH to run a shell script on a remote machine?Download a file with Android, and showing the progress in a ProgressDialogUsing node.js as a simple web serverHow can I update NodeJS and NPM to the next versions?ssh “permissions are too open” errorHow to download a file from server using SSH?Find the version of an installed npm packagenpm throws error without sudoWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?What is the --save option for npm install?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















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'










share|improve this question



















  • 1





    Hi Raphael! If you do not mind changing. I have an example here in stackoverflow in ssh2-sftp-client in this link: stackoverflow.com/questions/55297097/…

    – Carlos Jafet Neto
    Mar 26 at 17:48












  • man, I'm trying to do the way you teach in your answer, I even get to the files that interest me, but I'm not able to download the file because I get 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'

    – Raphael Melo De Lima
    Mar 26 at 18:54











  • Ok! Can you post your code?

    – Carlos Jafet Neto
    Mar 26 at 18:58











  • i just did it thx

    – Raphael Melo De Lima
    Mar 26 at 19:00






  • 1





    the '.' in front of the path is probably wrong

    – JeffRSon
    Mar 26 at 20:29

















0















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'










share|improve this question



















  • 1





    Hi Raphael! If you do not mind changing. I have an example here in stackoverflow in ssh2-sftp-client in this link: stackoverflow.com/questions/55297097/…

    – Carlos Jafet Neto
    Mar 26 at 17:48












  • man, I'm trying to do the way you teach in your answer, I even get to the files that interest me, but I'm not able to download the file because I get 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'

    – Raphael Melo De Lima
    Mar 26 at 18:54











  • Ok! Can you post your code?

    – Carlos Jafet Neto
    Mar 26 at 18:58











  • i just did it thx

    – Raphael Melo De Lima
    Mar 26 at 19:00






  • 1





    the '.' in front of the path is probably wrong

    – JeffRSon
    Mar 26 at 20:29













0












0








0








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'










share|improve this question
















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'






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





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





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');
);





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');
);






node.js ssh server download remote-access






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 18:59







Raphael Melo De Lima

















asked Mar 26 at 17:25









Raphael Melo De LimaRaphael Melo De Lima

1247 bronze badges




1247 bronze badges







  • 1





    Hi Raphael! If you do not mind changing. I have an example here in stackoverflow in ssh2-sftp-client in this link: stackoverflow.com/questions/55297097/…

    – Carlos Jafet Neto
    Mar 26 at 17:48












  • man, I'm trying to do the way you teach in your answer, I even get to the files that interest me, but I'm not able to download the file because I get 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'

    – Raphael Melo De Lima
    Mar 26 at 18:54











  • Ok! Can you post your code?

    – Carlos Jafet Neto
    Mar 26 at 18:58











  • i just did it thx

    – Raphael Melo De Lima
    Mar 26 at 19:00






  • 1





    the '.' in front of the path is probably wrong

    – JeffRSon
    Mar 26 at 20:29












  • 1





    Hi Raphael! If you do not mind changing. I have an example here in stackoverflow in ssh2-sftp-client in this link: stackoverflow.com/questions/55297097/…

    – Carlos Jafet Neto
    Mar 26 at 17:48












  • man, I'm trying to do the way you teach in your answer, I even get to the files that interest me, but I'm not able to download the file because I get 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'

    – Raphael Melo De Lima
    Mar 26 at 18:54











  • Ok! Can you post your code?

    – Carlos Jafet Neto
    Mar 26 at 18:58











  • i just did it thx

    – Raphael Melo De Lima
    Mar 26 at 19:00






  • 1





    the '.' in front of the path is probably wrong

    – JeffRSon
    Mar 26 at 20:29







1




1





Hi Raphael! If you do not mind changing. I have an example here in stackoverflow in ssh2-sftp-client in this link: stackoverflow.com/questions/55297097/…

– Carlos Jafet Neto
Mar 26 at 17:48






Hi Raphael! If you do not mind changing. I have an example here in stackoverflow in ssh2-sftp-client in this link: stackoverflow.com/questions/55297097/…

– Carlos Jafet Neto
Mar 26 at 17:48














man, I'm trying to do the way you teach in your answer, I even get to the files that interest me, but I'm not able to download the file because I get 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'

– Raphael Melo De Lima
Mar 26 at 18:54





man, I'm trying to do the way you teach in your answer, I even get to the files that interest me, but I'm not able to download the file because I get 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'

– Raphael Melo De Lima
Mar 26 at 18:54













Ok! Can you post your code?

– Carlos Jafet Neto
Mar 26 at 18:58





Ok! Can you post your code?

– Carlos Jafet Neto
Mar 26 at 18:58













i just did it thx

– Raphael Melo De Lima
Mar 26 at 19:00





i just did it thx

– Raphael Melo De Lima
Mar 26 at 19:00




1




1





the '.' in front of the path is probably wrong

– JeffRSon
Mar 26 at 20:29





the '.' in front of the path is probably wrong

– JeffRSon
Mar 26 at 20:29












1 Answer
1






active

oldest

votes


















1














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();





share|improve this answer

























  • i received this Promise <pending>

    – Raphael Melo De Lima
    Mar 26 at 19:35











  • Great! I will update the code!

    – Carlos Jafet Neto
    Mar 26 at 19:48











  • Now I have received a array of objects and are the ones I need to download

    – Raphael Melo De Lima
    Mar 26 at 20:06







  • 1





    Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

    – Carlos Jafet Neto
    Mar 26 at 20:21






  • 1





    You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

    – Carlos Jafet Neto
    Mar 26 at 20:50











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362968%2fdownload-file-using-ssh-with-simple-ssh-npm%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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();





share|improve this answer

























  • i received this Promise <pending>

    – Raphael Melo De Lima
    Mar 26 at 19:35











  • Great! I will update the code!

    – Carlos Jafet Neto
    Mar 26 at 19:48











  • Now I have received a array of objects and are the ones I need to download

    – Raphael Melo De Lima
    Mar 26 at 20:06







  • 1





    Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

    – Carlos Jafet Neto
    Mar 26 at 20:21






  • 1





    You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

    – Carlos Jafet Neto
    Mar 26 at 20:50
















1














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();





share|improve this answer

























  • i received this Promise <pending>

    – Raphael Melo De Lima
    Mar 26 at 19:35











  • Great! I will update the code!

    – Carlos Jafet Neto
    Mar 26 at 19:48











  • Now I have received a array of objects and are the ones I need to download

    – Raphael Melo De Lima
    Mar 26 at 20:06







  • 1





    Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

    – Carlos Jafet Neto
    Mar 26 at 20:21






  • 1





    You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

    – Carlos Jafet Neto
    Mar 26 at 20:50














1












1








1







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();





share|improve this answer















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();






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 20:55

























answered Mar 26 at 19:18









Carlos Jafet NetoCarlos Jafet Neto

5454 silver badges8 bronze badges




5454 silver badges8 bronze badges












  • i received this Promise <pending>

    – Raphael Melo De Lima
    Mar 26 at 19:35











  • Great! I will update the code!

    – Carlos Jafet Neto
    Mar 26 at 19:48











  • Now I have received a array of objects and are the ones I need to download

    – Raphael Melo De Lima
    Mar 26 at 20:06







  • 1





    Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

    – Carlos Jafet Neto
    Mar 26 at 20:21






  • 1





    You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

    – Carlos Jafet Neto
    Mar 26 at 20:50


















  • i received this Promise <pending>

    – Raphael Melo De Lima
    Mar 26 at 19:35











  • Great! I will update the code!

    – Carlos Jafet Neto
    Mar 26 at 19:48











  • Now I have received a array of objects and are the ones I need to download

    – Raphael Melo De Lima
    Mar 26 at 20:06







  • 1





    Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

    – Carlos Jafet Neto
    Mar 26 at 20:21






  • 1





    You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

    – Carlos Jafet Neto
    Mar 26 at 20:50

















i received this Promise <pending>

– Raphael Melo De Lima
Mar 26 at 19:35





i received this Promise <pending>

– Raphael Melo De Lima
Mar 26 at 19:35













Great! I will update the code!

– Carlos Jafet Neto
Mar 26 at 19:48





Great! I will update the code!

– Carlos Jafet Neto
Mar 26 at 19:48













Now I have received a array of objects and are the ones I need to download

– Raphael Melo De Lima
Mar 26 at 20:06






Now I have received a array of objects and are the ones I need to download

– Raphael Melo De Lima
Mar 26 at 20:06





1




1





Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

– Carlos Jafet Neto
Mar 26 at 20:21





Yes. If you now already the file you do not even need the loop. I will update with this extra solution.

– Carlos Jafet Neto
Mar 26 at 20:21




1




1





You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

– Carlos Jafet Neto
Mar 26 at 20:50






You would need to make a get request to the browser with the full path of your file! You can do this with the request module. Take a look: npmjs.com/package/request

– Carlos Jafet Neto
Mar 26 at 20:50









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362968%2fdownload-file-using-ssh-with-simple-ssh-npm%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript