[node]nodejs ssh 2テスト
43409 ワード
2.1 exec(コンテキストなし、リモート実行コマンド)
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
conn.exec('uname -a', function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: '192.168.6.150',
port: 22,
username: 'slview',
password: 'slview#2012'
//privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
});
D:\fk\work
ode\ssh>node tsshexec.js
STDOUT: Linux master 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x
86_64 x86_64 x86_64 GNU/Linux
: , , ondata ,
2.2shell ( )
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
conn.shell(function(err, stream) {
if (err) throw err;
stream.on('close', function() {
console.log('Stream :: close');
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
stream.end('cd test
ls -l
exit
');
});
}).connect({
host: '192.168.6.150',
port: 22,
username: 'slview',
password: 'slview#2012'
});
2.2.1 ssh 。
D:\fk\work
ode\ssh>node tsshshdev.js
Client :: ready
STDOUT: Last login: Tue Oct 25 17:43:58 2016 from 219.143.200.152
STDOUT: ssh -V
exit
STDOUT: -bash-4.1$ ssh -V
STDOUT: OpenSSH_7.1p1, OpenSSL 1.0.1m 19 Mar 2015
-bash-4.1$ exit
STDOUT: logout
Stream :: close
D:\fk\work
ode\ssh>
2.2.2
,serverhostkey ( debug, )
\ssh
ode_modules\ssh2-streams\lib\constant.js
var DEFAULT_SERVER_HOST_KEY = [
'ssh-rsa'
];
ssh-dss( はsuportの で、デフォルトは いていません)var DEFAULT_SERVER_HOST_KEY = [
'ssh-rsa',
'ssh-dss'
];
ログインも しました.var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
conn.shell(function(err, stream) {
if (err) throw err;
stream.on('close', function() {
console.log('Stream :: close');
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
//huawei
//stream.write('display version
');
//stream.write('screen-length 0 temporary
'); //more
//stream.write('?'); //more
//stream.end('logout
');
//iox
//stream.write('show version
');
//stream.end('exit
');
//ios
stream.write('en
show version
');
stream.end('exit
');
});
}).connect({
host: '192.168.6.150',
//huawei ok
//port: 10022,
//iox ok
//port: 10023,
//ios ok
port: 10024,
username: 'admin',
password: 'admin',
//debug: function(str) { console.log('%j', str); }
//password: 'Wygmwbl@0431'
});