【问题标题】:Download file using ssh with simple-ssh npm使用 ssh 和 simple-ssh npm 下载文件
【发布时间】:2019-03-26 17:25:07
【问题描述】:

我需要使用 ssh 从远程到本地下载服务器上的文件。 我正在使用来自 npm link to npm module 的 simple-ssh 模块

我可以更新服务器上已经存在的文件,但是我需要下载这样的文件

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

有人可以帮帮我吗?我迷路了

在@Carlos Jafet Neto 的帮助下,我的代码已经改变,现在:

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

但是sftp.get 给我带来了以下错误

{ [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' }

【问题讨论】:

  • 嗨拉斐尔!如果你不介意改变。我在此链接中的 ssh2-sftp-client 的 stackoverflow 中有一个示例:stackoverflow.com/questions/55297097/…
  • 伙计,我正在尝试按照您在回答中教授的方式进行操作,我什至可以访问我感兴趣的文件,但由于出现以下错误,我无法下载该文件:{ [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' }
  • 好的!你能发布你的代码吗?
  • 我刚刚做到了,谢谢
  • '.'前面的路径可能是错误的

标签: node.js ssh server download remote-access


【解决方案1】:

尝试只获取文件夹中的文件而不使用变量,使用路径作为字符串来消除错误。

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

获取单个文件:

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

使用它来关闭 ftp 连接:

sftp.end();

【讨论】:

  • 我收到了这个Promise { <pending> }
  • 现在我收到了一组对象,是我需要下载的对象
  • 太棒了!就是这样!现在你可以做一个 files.forEach(x =>....这里的 x 将是数组的每个元素,你可以像这样获取每个文件名: let remoteFilePath = '/' + x.name; 我会更新那里的代码!
  • 是的。如果你现在已经是文件,你甚至不需要循环。我将使用这个额外的解决方案进行更新。
  • 您需要使用文件的完整路径向浏览器发出获取请求!您可以使用请求模块执行此操作。看一看:npmjs.com/package/request
猜你喜欢
  • 2021-09-14
  • 2015-06-13
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2021-10-13
  • 2016-03-28
  • 2012-03-14
相关资源
最近更新 更多