linux - How to SSH into a server from a node.js application? -
var exec = require('child_process').exec; exec('ssh my_ip',function(err,stdout,stderr){ console.log(err,stdout,stderr); });
this freezes - guess, because ssh my_ip
asks password, interactive, etc. how correctly?
there's node.js module written perform tasks in ssh using node called ssh2 mscdex. can found here. example you're wanting (from readme) be:
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: '192.168.100.100', port: 22, username: 'frylock', privatekey: require('fs').readfilesync('/here/is/my/key') });
Comments
Post a Comment