使用 ssh 和 simple-ssh npm 下载文件
Download file using ssh with simple-ssh npm
我需要使用 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' }
尝试首先仅获取文件夹中的文件而不使用变量,使用您的路径作为字符串来消除错误。
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();
我需要使用 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' }
尝试首先仅获取文件夹中的文件而不使用变量,使用您的路径作为字符串来消除错误。
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();