telnet-client
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -25,10 +25,10 @@ // Node.js Telnet client | ||
self.timeout = (typeof opts.timeout !== 'undefined' ? opts.timeout : 500) | ||
self.shellPrompt = (typeof opts.shellPrompt !== 'undefined' | ||
? opts.shellPrompt : /(?:\/ )?#\s/) | ||
self.loginPrompt = (typeof opts.loginPrompt !== 'undefined' | ||
? opts.loginPrompt : /login[: ]*$/i) | ||
self.passwordPrompt = (typeof opts.passwordPrompt !== 'undefined' | ||
? opts.passwordPrompt : /Password: /i) | ||
self.failedLoginPrompt = (typeof opts.failedLoginPrompt !== 'undefined' | ||
? opts.failedLoginPrompt : undefined) | ||
// Set prompt regex defaults | ||
self.shellPrompt = checkRegExp(opts.shellPrompt, /(?:\/ )?#\s/) | ||
self.loginPrompt = checkRegExp(opts.loginPrompt, /login[: ]*$/i) | ||
self.passwordPrompt = checkRegExp(opts.passwordPrompt, /Password: /i) | ||
self.failedLoginPrompt = checkRegExp(opts.failedLoginPrompt, undefined) | ||
self.debug = (typeof opts.debug !== 'undefined' ? opts.debug : false) | ||
self.username = (typeof opts.username !== 'undefined' ? opts.username : 'root') | ||
@@ -91,5 +91,5 @@ self.password = (typeof opts.password !== 'undefined' ? opts.password : 'guest') | ||
if (opts && opts instanceof Object) { | ||
self.shellPrompt = opts.shellPrompt || self.shellPrompt | ||
self.loginPrompt = opts.loginPrompt || self.loginPrompt | ||
self.failedLoginPrompt = opts.failedLoginPrompt || self.failedLoginPrompt | ||
self.shellPrompt = checkRegExp(opts.shellPrompt, self.shellPrompt) | ||
self.loginPrompt = checkRegExp(opts.loginPrompt, self.loginPrompt) | ||
self.failedLoginPrompt = checkRegExp(opts.failedLoginPrompt, self.failedLoginPrompt) | ||
self.timeout = opts.timeout || self.timeout | ||
@@ -155,4 +155,4 @@ self.irs = opts.irs || self.irs | ||
var stringData = chunk.toString() | ||
var promptIndex = stringData.search(regexEscape(telnetObj.shellPrompt)) | ||
var promptIndex = stringData.search(telnetObj.shellPrompt) | ||
if (promptIndex !== -1) { | ||
@@ -166,7 +166,7 @@ telnetObj.shellPrompt = 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' | ||
@@ -185,6 +185,5 @@ login(telnetObj, 'password') | ||
telnetObj.stringData += stringData | ||
promptIndex = stringData.search(regexEscape(telnetObj.shellPrompt)) | ||
promptIndex = stringData.indexOf(telnetObj.shellPrompt) | ||
if (promptIndex === -1 && stringData.length !== 0) { | ||
if (stringData.search(regexEscape(telnetObj.pageSeparator)) !== -1) { | ||
if (stringData.search(telnetObj.pageSeparator) !== -1) { | ||
telnetObj.telnetSocket.write(Buffer('20', 'hex')) | ||
@@ -197,5 +196,4 @@ } | ||
telnetObj.cmdOutput = telnetObj.stringData.split(telnetObj.irs) | ||
for (var i = 0; i < telnetObj.cmdOutput.length; i++) { | ||
if (telnetObj.cmdOutput[i].search(regexEscape(telnetObj.pageSeparator)) !== -1) { | ||
if (telnetObj.cmdOutput[i].search(telnetObj.pageSeparator) !== -1) { | ||
telnetObj.cmdOutput[i] = telnetObj.cmdOutput[i].replace(telnetObj.pageSeparator, '') | ||
@@ -246,6 +244,22 @@ if (telnetObj.cmdOutput[i].length === 0) telnetObj.cmdOutput.splice(i, 1) | ||
function regexEscape(string) { | ||
return string.toString().replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') | ||
function checkRegExp(str, def){ | ||
if (!str) { | ||
return def | ||
} | ||
if (str instanceof RegExp) { | ||
// if they sent a regex obj, use it | ||
return str | ||
} | ||
else { | ||
// otherwise, convert their string to the equivalent regex | ||
try { | ||
return new RegExp(str) | ||
} | ||
catch(error) { | ||
if (self.debug) console.error('Invalid RegExp:', str) | ||
} | ||
} | ||
} | ||
module.exports = Telnet |
@@ -8,3 +8,3 @@ { | ||
}, | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"main": "./lib/telnet-client.js", | ||
@@ -11,0 +11,0 @@ "engine": "node >= 0.10.29", |
@@ -35,3 +35,3 @@ [![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/mkozjak/node-telnet-client/blob/master/LICENSE) | ||
connection.on('ready', function(prompt) { | ||
connection.exec(cmd, function(response) { | ||
connection.exec(cmd, function(err, response) { | ||
console.log(response); | ||
@@ -38,0 +38,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17145
248