Skip to content

Commit

Permalink
changed regexing around
Browse files Browse the repository at this point in the history
  • Loading branch information
srgraham committed Nov 18, 2015
1 parent c1b07e3 commit 03b5c5e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ lib-cov/
*.log
*.swp
*.swo
*.iml
.idea/
76 changes: 62 additions & 14 deletions lib/telnet-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,59 @@ Telnet.prototype.connect = function(opts) {
var host = (typeof opts.host !== 'undefined' ? opts.host : '127.0.0.1');
var port = (typeof opts.port !== 'undefined' ? opts.port : 23);
this.timeout = (typeof opts.timeout !== 'undefined' ? opts.timeout : 500);
this.shellPrompt = (typeof opts.shellPrompt !== 'undefined'
? opts.shellPrompt : /(?:\/ )?#\s/);
this.loginPrompt = (typeof opts.loginPrompt !== 'undefined'
? opts.loginPrompt : /login[: ]*$/i);
this.passwordPrompt = (typeof opts.passwordPrompt !== 'undefined'
? opts.passwordPrompt : /Password: /i);
this.failedLoginPrompt = (typeof opts.failedLoginPrompt !== 'undefined'
? opts.failedLoginPrompt : undefined);

// Set prompt regex defaults
this.shellPrompt = /(?:\/ )?#\s/;
this.loginPrompt = /login[: ]*$/i;
this.passwordPrompt = /Password: /i;
this.failedLoginPrompt = undefined;


if (opts.shellPrompt){
if (opts.shellPrompt instanceof RegExp) {
// if they sent a regex obj, use it
self.shellPrompt = opts.shellPrompt;
} else {
// otherwise, convert their string to the equivalent regex
self.shellPrompt = new RegExp(regexEscape(opts.shellPrompt));
}
}
if (opts.loginPrompt){
if (opts.loginPrompt instanceof RegExp) {
// if they sent a regex obj, use it
self.loginPrompt = opts.loginPrompt;
} else {
// otherwise, convert their string to the equivalent regex
self.loginPrompt = new RegExp(regexEscape(opts.loginPrompt));
}
}
if (opts.passwordPrompt){
if (opts.passwordPrompt instanceof RegExp) {
// if they sent a regex obj, use it
self.passwordPrompt = opts.passwordPrompt;
} else {
// otherwise, convert their string to the equivalent regex
self.passwordPrompt = new RegExp(regexEscape(opts.passwordPrompt));
}
}
if (opts.failedLoginPrompt){
if (opts.failedLoginPrompt instanceof RegExp) {
// if they sent a regex obj, use it
self.failedLoginPrompt = opts.failedLoginPrompt;
} else {
// otherwise, convert their string to the equivalent regex
self.failedLoginPrompt = new RegExp(regexEscape(opts.failedLoginPrompt));
}
}

//this.shellPrompt = (typeof opts.shellPrompt !== 'undefined'
// ? opts.shellPrompt : /(?:\/ )?#\s/);
//this.loginPrompt = (typeof opts.loginPrompt !== 'undefined'
// ? opts.loginPrompt : /login[: ]*$/i);
//this.passwordPrompt = (typeof opts.passwordPrompt !== 'undefined'
// ? opts.passwordPrompt : /Password: /i);
//this.failedLoginPrompt = (typeof opts.failedLoginPrompt !== 'undefined'
// ? opts.failedLoginPrompt : undefined);
this.username = (typeof opts.username !== 'undefined' ? opts.username : 'root');
this.password = (typeof opts.password !== 'undefined' ? opts.password : 'guest');
this.irs = (typeof opts.irs !== 'undefined' ? opts.irs : '\r\n');
Expand Down Expand Up @@ -99,6 +144,7 @@ Telnet.prototype.exec = function(cmd, opts, callback) {
if (callback && self.cmdOutput !== 'undefined') {
callback(self.cmdOutput.join('\n'));
}
// FIXME: should this be callback()?
else if (callback && self.cmdOutput === 'undefined') callback;

// reset stored response
Expand All @@ -117,6 +163,7 @@ Telnet.prototype.destroy = function() {
}

function parseData(chunk, telnetObj) {
//console.log('chunk', chunk.toString())
var promptIndex = '';

if (chunk[0] === 255 && chunk[1] !== 255) {
Expand All @@ -133,19 +180,20 @@ function parseData(chunk, telnetObj) {

if (telnetObj.telnetState === 'getprompt') {
var stringData = chunk.toString();
var promptIndex = stringData.search(regexEscape(telnetObj.shellPrompt));
var promptIndex = stringData.search(telnetObj.shellPrompt);

if (promptIndex !== -1) {
telnetObj.shellPrompt = stringData.substring(promptIndex);
//telnetObj.shellPrompt = stringData.substring(promptIndex);
telnetObj.telnetState = 'sendcmd';
telnetObj.stringData = '';
telnetObj.emit('ready', telnetObj.shellPrompt);
//telnetObj.emit('ready', telnetObj.shellPrompt);
telnetObj.emit('ready', stringData.substring(promptIndex));
}
else if (stringData.search(regexEscape(telnetObj.loginPrompt)) !== -1) {
else if (stringData.search(telnetObj.loginPrompt) !== -1) {
telnetObj.telnetState = 'login';
login(telnetObj, 'username');
}
else if (stringData.search(regexEscape(telnetObj.passwordPrompt)) !== -1) {
else if (stringData.search(telnetObj.passwordPrompt) !== -1) {
telnetObj.telnetState = 'login';
login(telnetObj, 'password');
}
Expand All @@ -159,7 +207,7 @@ function parseData(chunk, telnetObj) {
else if (telnetObj.telnetState === 'response') {
var stringData = chunk.toString();
telnetObj.stringData += stringData;
promptIndex = stringData.search(regexEscape(telnetObj.shellPrompt));
promptIndex = stringData.search(telnetObj.shellPrompt);

if (promptIndex === -1 && stringData.length !== 0) {
if (stringData.search(regexEscape(telnetObj.pageSeparator)) !== -1) {
Expand Down

0 comments on commit 03b5c5e

Please sign in to comment.