Guest User

Untitled

a guest
Dec 4th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. var SshClient = require('ssh2').Client;
  2.  
  3. /**
  4. * @example
  5. *
  6. * var sshExpect = require('./sshExpect');
  7. * sshExpect({
  8. * host: '10.79.53.164',
  9. * port: 22,
  10. * username: 'admin',
  11. * password: 'default',
  12. * timeout: 6000,
  13. * debug: false
  14. * }, function(stream) {
  15. * stream.expect(/[$#]/, function(data, next) {
  16. * stream.write('passwd root\n');
  17. * next();
  18. * });
  19. * stream.expect('New password:', function(data, next) {
  20. * stream.write('default\n');
  21. * next();
  22. * });
  23. * stream.expect('Retype new password:', function(data, next) {
  24. * stream.write('default\n');
  25. * next();
  26. * });
  27. * stream.expect(/[$#]/, function(data, next) {
  28. * stream.close();
  29. * });
  30. * }, function(err) {
  31. * if (err) throw err;
  32. * });
  33. */
  34.  
  35. module.exports = function sshExpect(config, expectFunc, callback) {
  36.  
  37. var tty = config.tty || {};
  38.  
  39. var sshc = new SshClient();
  40.  
  41. sshc.on('ready', function() {
  42. sshc.shell({
  43. rows: tty.rows || 50,
  44. cols: tty.cols || 200
  45. }, function(err, stream) {
  46. if (err) callback(err);
  47.  
  48. stream.on('close', function() {
  49. sshc.end();
  50. });
  51.  
  52. var _data;
  53. var _expects = [];
  54.  
  55. stream.ondata = function(data) {
  56. if (config.debug) {
  57. process.stdout.write(data);
  58. }
  59.  
  60. _data = (_data || '') + data;
  61. if (_expects.length > 0) {
  62. var matched = (typeof _expects[0].pattern === 'string') ? (_data.indexOf(_expects[0].pattern) !== -1) : _data.match(_expects[0].pattern);
  63. if (matched) {
  64. var expect = _expects.shift();
  65. clearTimeout(expect.timeoutId);
  66. expect.fn(_data, function() {
  67. stream.waitExpect();
  68. });
  69. }
  70. }
  71. };
  72.  
  73. stream.on('data', stream.ondata);
  74. stream.stderr.on('data', stream.ondata);
  75.  
  76. stream.waitExpect = function() {
  77. _data = '';
  78. if (_expects.length > 0) {
  79. _expects[0].timeoutId = setTimeout(function() {
  80. var err = new Error('timeout.\n expect: ' + _expects[0].pattern + '\n actual: ' + _data);
  81. err.expect = _expects[0];
  82. sshc.emit('expect.error', err);
  83. }, config.timeout || 60000);
  84. }
  85. };
  86.  
  87. stream.expect = function(pattern, fn) {
  88. _expects.push({
  89. pattern: pattern,
  90. fn: fn
  91. });
  92. if (_expects.length === 1) {
  93. stream.waitExpect();
  94. }
  95. };
  96.  
  97. sshc.emit('expect.ready', stream);
  98. });
  99. });
  100.  
  101. sshc.on('expect.ready', function(stream) {
  102. expectFunc(stream);
  103. });
  104.  
  105. sshc.on('expect.error', function(err) {
  106. callback(err);
  107. });
  108. sshc.on('close', function(code) {
  109. callback(null);
  110. });
  111.  
  112. if (config.debug) {
  113. console.log('connecting %s@%s', config.username, config.host);
  114. }
  115. sshc.connect({
  116. host: config.host,
  117. port: config.port || 22,
  118. username: config.username || 'root',
  119. password: config.password || 'default'
  120. });
  121. };
Add Comment
Please, Sign In to add comment