[node]nodejs ssh2测试

2.1exec(无上下文,远程执行命令)
   
   
  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\node\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\nls -l\nexit\n');
  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\node\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\node\ssh>

2.2.2连接设备模拟器
有点小问题,serverhostkey格式不对(开启debug,定位问题)
\ssh\node_modules\ssh2-streams\lib\constant.js
   
   
  1. var DEFAULT_SERVER_HOST_KEY = [
  2. 'ssh-rsa'
  3. ];
补充ssh-dss(原来是在suport里面,默认未开启)
   
   
  1. var DEFAULT_SERVER_HOST_KEY = [
  2. 'ssh-rsa',
  3. 'ssh-dss'
  4. ];
登录居然也一次过
   
   
  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. //huawei
  16. //stream.write('display version\n');
  17. //stream.write('screen-length 0 temporary\n'); //more的问题
  18. //stream.write('?'); //more的问题
  19. //stream.end('logout\n');
  20. //iox
  21. //stream.write('show version\n');
  22. //stream.end('exit\n');
  23. //ios
  24. stream.write('en\nshow version\n');
  25. stream.end('exit\n');
  26. });
  27. }).connect({
  28. host: '192.168.6.150',
  29. //huawei 登录ok
  30. //port: 10022,
  31. //iox 登录ok
  32. //port: 10023,
  33. //ios 登录ok
  34. port: 10024,
  35. username: 'admin',
  36. password: 'admin',
  37. //debug: function(str) { console.log('%j', str); }
  38. //password: 'Wygmwbl@0431'
  39. });


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值