nodejs ssh-exec 将一些数据通过管道传输到远程进程

nodejs ssh-exec pipe some data to the remote process

我想将使用 npm ssh-exec 的命令输出传递给变量(或文件,等等)而不是标准输出。下面这个适用于 stdout

process.stdin
    .pipe(exec('ls -l', config.user_host))
    .pipe(process.stdout);

ssh-exec 的文档如下所述。那么我如何将它准确地传递给远程进程(buff 变量、文件)而不是标准输出?

//if you want to pipe some data to the remote process
process.stdin
    .pipe(exec('echo try typing something; cat -', 'ubuntu@my-remote.com'))
    .pipe(process.stdout)

这样你就可以将输出写入文件:

var fs = require('fs')
var exec = require('ssh-exec')

file = fs.createWriteStream('output.txt');
process.stdin
    .pipe(exec('echo try typing something; cat -', 'ubuntu@my-remote.com'))
    .pipe(file)

将命令输出到缓冲区:

var exec = require('ssh-exec')

stream = process.stdin
    .pipe(exec('echo try typing something; cat -', 'ubuntu@my-remote.com'))

var buffers = [];
stream.on('data', function(buffer) {
  buffers.push(buffer);
});
stream.on('end', function() {
  var buffer = Buffer.concat(buffers);
  console.log(buffer.toString());
});