nodejs ssh-exec将一些数据传递给远程进程

时间:2015-06-26 20:27:59

标签: node.js ssh

我想使用npm ssh-exec将命令输出传递给变量(或文件,无论如何),但不传递给stdout。这适用于stdout

array <- Array.prototype <- Object.prototype <- null

ssh-exec的文档如下所示。那么如何将它完全传递给远程进程(buff变量,文件)而不是stdout?

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

1 个答案:

答案 0 :(得分:1)

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

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