为了操作ssh on grunt插件,使用ssh2,我已经创建了一个任务,但是在connect()之后,on事件发生,进程会被终止。这可能是什么原因造成的?
在你不想使用 grunt.js 的普通程序中没有问题。. .
咕噜文件
ssh2sample: {
default_options: {
options: {
'host': 'xxx.xxx.xxx.xxx',
'port': 22,
'username': 'names',
'privateKey': 'keys',
'passphrase': 'pass'
},
任务/ssh2sample.js
'use strict';
module.exports = function(grunt) {
// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks
grunt.registerMultiTask('ssh2sample', 'The best Grunt plugin ever.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var Connection = require('ssh2');
var options = this.options();
options.privateKey = require('fs').readFileSync(options.privateKey);
var c = new Connection();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
console.log('Connection :: ready');
c.exec('uptime', function(err, stream) {
//if (err) throw err;
stream.on('data', function(data, extended) {
console.log(extended === 'stderr' ? 'STDERR: ' : 'STDOUT: '+ data);
});
stream.on('end', function() {
console.log('Stream :: EOF');
});
stream.on('close', function() {
console.log('Stream :: close');
});
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
c.end();
});
});
});
c.on('error', function(err) {
console.log('Connection :: error :: ' + err);
});
c.on('end', function() {
console.log('Connection :: end');
});
c.on('close', function(had_error) {
console.log('Connection :: close');
});
c.connect(options);
}
});
没问题 ssh2connect
var Connection = require('ssh2');
var options = this.options();
options.privateKey = require('fs').readFileSync(options.privateKey);
var c = new Connection();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
console.log('Connection :: ready');
c.exec('uptime', function(err, stream) {
//if (err) throw err;
stream.on('data', function(data, extended) {
console.log(extended === 'stderr' ? 'STDERR: ' : 'STDOUT: '+ data);
});
stream.on('end', function() {
console.log('Stream :: EOF');
});
stream.on('close', function() {
console.log('Stream :: close');
});
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
c.end();
});
});
});
c.on('error', function(err) {
console.log('Connection :: error :: ' + err);
});
c.on('end', function() {
console.log('Connection :: end');
});
c.on('close', function(had_error) {
console.log('Connection :: close');
});
var key = require(fs).readdirSync(keys);
c.connect({
'host': 'xxx.xxx.xxx.xxx',
'port': 22,
'username': 'names',
'privateKey': key,
'passphrase': 'pass'
});