Skip to content

Instantly share code, notes, and snippets.

@lifuzu
Created August 6, 2013 01:58
Show Gist options
  • Save lifuzu/6161363 to your computer and use it in GitHub Desktop.
Save lifuzu/6161363 to your computer and use it in GitHub Desktop.
var Connection = require('ssh2');
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({
host: 'dsvsb1amq01.sjc1700.bnweb.user.bn',
port: 22,
username: 'rlee',
//privateKey: require('fs').readFileSync('/home/rlee/key')
password: process.env.MYDOMAINPASSWORD
});
// example output:
// Connection :: connect
// Connection :: ready
// STDOUT: 17:41:15 up 22 days, 18:09, 1 user, load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Connection :: end
// Connection :: close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment