[node]nodejs ssh 2テスト

43409 ワード

2.1 exec(コンテキストなし、リモート実行コマンド)
 
    
  1. var Client = require('ssh2').Client;
  2. var conn = new Client();
  3. conn.on('ready', function() {
  4. conn.exec('uname -a', function(err, stream) {
  5. if (err) throw err;
  6. stream.on('close', function(code, signal) {
  7. conn.end();
  8. }).on('data', function(data) {
  9. console.log('STDOUT: ' + data);
  10. }).stderr.on('data', function(data) {
  11. console.log('STDERR: ' + data);
  12. });
  13. });
  14. }).connect({
  15. host: '192.168.6.150',
  16. port: 22,
  17. username: 'slview',
  18. password: 'slview#2012'
  19. //privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
  20. });
 
    
  1. D:\fk\work
    ode\ssh
    >node tsshexec.js
  2. STDOUT: Linux master 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x
  3. 86_64 x86_64 x86_64 GNU/Linux
: , , ondata ,

2.2shell ( )
 
    
  1. var Client = require('ssh2').Client;
  2. var conn = new Client();
  3. conn.on('ready', function() {
  4. console.log('Client :: ready');
  5. conn.shell(function(err, stream) {
  6. if (err) throw err;
  7. stream.on('close', function() {
  8. console.log('Stream :: close');
  9. conn.end();
  10. }).on('data', function(data) {
  11. console.log('STDOUT: ' + data);
  12. }).stderr.on('data', function(data) {
  13. console.log('STDERR: ' + data);
  14. });
  15. stream.end('cd test
    ls -l
    exit
    '
    );
  16. });
  17. }).connect({
  18. host: '192.168.6.150',
  19. port: 22,
  20. username: 'slview',
  21. password: 'slview#2012'
  22. });

2.2.1 ssh 。
 
    
  1. D:\fk\work
    ode\ssh
    >node tsshshdev.js
  2. Client :: ready
  3. STDOUT: Last login: Tue Oct 25 17:43:58 2016 from 219.143.200.152
  4. STDOUT: ssh -V
  5. exit
  6. STDOUT: -bash-4.1$ ssh -V
  7. STDOUT: OpenSSH_7.1p1, OpenSSL 1.0.1m 19 Mar 2015
  8. -bash-4.1$ exit
  9. STDOUT: logout
  10. Stream :: close
  11. D:\fk\work
    ode\ssh
    >

2.2.2
,serverhostkey ( debug, )
\ssh
ode_modules\ssh2-streams\lib\constant.js
 
    
  1. var DEFAULT_SERVER_HOST_KEY = [
  2. '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' });