Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var SshClient = require('ssh2').Client;
- /**
- * @example
- *
- * var sshExpect = require('./sshExpect');
- * sshExpect({
- * host: '10.79.53.164',
- * port: 22,
- * username: 'admin',
- * password: 'default',
- * timeout: 6000,
- * debug: false
- * }, function(stream) {
- * stream.expect(/[$#]/, function(data, next) {
- * stream.write('passwd root\n');
- * next();
- * });
- * stream.expect('New password:', function(data, next) {
- * stream.write('default\n');
- * next();
- * });
- * stream.expect('Retype new password:', function(data, next) {
- * stream.write('default\n');
- * next();
- * });
- * stream.expect(/[$#]/, function(data, next) {
- * stream.close();
- * });
- * }, function(err) {
- * if (err) throw err;
- * });
- */
- module.exports = function sshExpect(config, expectFunc, callback) {
- var tty = config.tty || {};
- var sshc = new SshClient();
- sshc.on('ready', function() {
- sshc.shell({
- rows: tty.rows || 50,
- cols: tty.cols || 200
- }, function(err, stream) {
- if (err) callback(err);
- stream.on('close', function() {
- sshc.end();
- });
- var _data;
- var _expects = [];
- stream.ondata = function(data) {
- if (config.debug) {
- process.stdout.write(data);
- }
- _data = (_data || '') + data;
- if (_expects.length > 0) {
- var matched = (typeof _expects[0].pattern === 'string') ? (_data.indexOf(_expects[0].pattern) !== -1) : _data.match(_expects[0].pattern);
- if (matched) {
- var expect = _expects.shift();
- clearTimeout(expect.timeoutId);
- expect.fn(_data, function() {
- stream.waitExpect();
- });
- }
- }
- };
- stream.on('data', stream.ondata);
- stream.stderr.on('data', stream.ondata);
- stream.waitExpect = function() {
- _data = '';
- if (_expects.length > 0) {
- _expects[0].timeoutId = setTimeout(function() {
- var err = new Error('timeout.\n expect: ' + _expects[0].pattern + '\n actual: ' + _data);
- err.expect = _expects[0];
- sshc.emit('expect.error', err);
- }, config.timeout || 60000);
- }
- };
- stream.expect = function(pattern, fn) {
- _expects.push({
- pattern: pattern,
- fn: fn
- });
- if (_expects.length === 1) {
- stream.waitExpect();
- }
- };
- sshc.emit('expect.ready', stream);
- });
- });
- sshc.on('expect.ready', function(stream) {
- expectFunc(stream);
- });
- sshc.on('expect.error', function(err) {
- callback(err);
- });
- sshc.on('close', function(code) {
- callback(null);
- });
- if (config.debug) {
- console.log('connecting %s@%s', config.username, config.host);
- }
- sshc.connect({
- host: config.host,
- port: config.port || 22,
- username: config.username || 'root',
- password: config.password || 'default'
- });
- };
Add Comment
Please, Sign In to add comment