telnet-client
Advanced tools
Comparing version 0.12.1 to 0.12.2
80
app.js
@@ -1,20 +0,72 @@ | ||
var telnet = require('./lib') | ||
var telnet = require('./lib/index') | ||
var telnet_server = require('telnet') | ||
var connection = new telnet() | ||
var srv = telnet_server.createServer(function(c) { | ||
c.write(new Buffer("BusyBox v1.19.2 () built-in shell (ash)\n" | ||
+ "Enter 'help' for a list of built-in commands.\n\n/ # ", 'ascii')) | ||
connection.connect({ | ||
c.on('data', function() { | ||
c.write(new Buffer("uptime\r\n23:14 up 1 day, 21:50, 6 users, " | ||
+ "load averages: 1.41 1.43 1.41\r\n", 'ascii')) | ||
c.write(new Buffer("/ # ", 'ascii')) | ||
}) | ||
}) | ||
srv.listen(2323, function() { | ||
var connection = new telnet() | ||
var params = { | ||
host: '127.0.0.1', | ||
port: 2323, | ||
shellPrompt: '/ # ', | ||
timeout: 1500 | ||
} | ||
connection.on('ready', function(prompt) { | ||
connection.exec('uptime', function(err, resp) { | ||
console.log(err, resp) | ||
connection.end() | ||
}) | ||
}) | ||
connection.connect(params); | ||
}) | ||
/* | ||
var params = { | ||
host: '127.0.0.1', | ||
port: 1337, | ||
execTimeout: 1000, | ||
port: 8080, | ||
negotiationMandatory: false | ||
}).then(function() { | ||
connection.send('hi') | ||
.then(function(res) { | ||
console.log(res) | ||
}, function(error) { | ||
console.log(error) | ||
} | ||
c.on('failedlogin', function() { | ||
console.log('login failed') | ||
}) | ||
c.on("connect", function() { | ||
console.log('sending data') | ||
c.send('something', { | ||
ors: '\r\n', | ||
waitfor: '\n' | ||
}, function(error, data) { | ||
console.log('received a response', data) | ||
}) | ||
}) | ||
c.connect(params).then(function() { | ||
setInterval(function() { | ||
console.log('sending') | ||
c.send('something', { | ||
ors: '\r\n', | ||
waitfor: '\n' | ||
}, function(error, data) { | ||
console.log('received a response:', error, data) | ||
}) | ||
.catch(function(error) { | ||
console.log(error) | ||
}) | ||
}, 2000) | ||
}) | ||
*/ | ||
@@ -42,2 +42,3 @@ // Node.js Telnet client | ||
this.sendTimeout = (typeof opts.sendTimeout !== 'undefined' ? opts.sendTimeout : 2000) | ||
this.maxBufferLength = (typeof opts.maxBufferLength !== 'undefined' ? opts.maxBufferLength : 1048576) | ||
@@ -49,3 +50,3 @@ this.telnetSocket = net.createConnection({ | ||
this.telnetState = 'start' | ||
this.stringData = '' | ||
this.inputBuffer = '' | ||
@@ -104,2 +105,3 @@ this.emit('connect') | ||
this.echoLines = opts.echoLines || this.echoLines | ||
this.maxBufferLength = opts.maxBufferLength || this.maxBufferLength | ||
} | ||
@@ -123,4 +125,4 @@ | ||
if (this.cmdOutput !== 'undefined') { | ||
resolve(this.cmdOutput.join('\n')) | ||
if (this.response !== 'undefined') { | ||
resolve(this.response.join('\n')) | ||
} | ||
@@ -130,3 +132,3 @@ else reject(new Error('invalid response')) | ||
// reset stored response | ||
this.stringData = '' | ||
this.inputBuffer = '' | ||
@@ -137,6 +139,23 @@ // set state back to 'standby' for possible telnet server push data | ||
this.once('bufferexceeded', () => { | ||
if (execTimeout !== null) { | ||
clearTimeout(execTimeout) | ||
} | ||
if (!this.inputBuffer) return reject(new Error('response not received')) | ||
resolve(this.inputBuffer) | ||
// reset stored response | ||
this.inputBuffer = '' | ||
// set state back to 'standby' for possible telnet server push data | ||
this.telnetState = 'standby' | ||
}) | ||
if (this.execTimeout) { | ||
execTimeout = setTimeout(() => { | ||
execTimeout = null | ||
if (!this.cmdOutput) return reject(new Error('response not received')) | ||
if (!this.response) return reject(new Error('response not received')) | ||
}, this.execTimeout) | ||
@@ -155,2 +174,3 @@ } | ||
this.sendTimeout = opts.timeout || this.sendTimeout | ||
this.maxBufferLength = opts.maxBufferLength || this.maxBufferLength | ||
@@ -202,6 +222,6 @@ data += this.ors | ||
_parseData(chunk, callback) { | ||
var promptIndex = '' | ||
let promptIndex = '' | ||
if (chunk[0] === 255 && chunk[1] !== 255) { | ||
this.stringData = '' | ||
this.inputBuffer = '' | ||
const negReturn = this._negotiate(chunk) | ||
@@ -218,5 +238,5 @@ | ||
if (this.telnetState === 'getprompt') { | ||
var stringData = chunk.toString() | ||
const stringData = chunk.toString() | ||
var promptIndex = utils.search(stringData, this.shellPrompt) | ||
let promptIndex = utils.search(stringData, this.shellPrompt) | ||
@@ -247,3 +267,3 @@ if (utils.search(stringData, this.loginPrompt) !== -1) { | ||
this.telnetState = 'standby' | ||
this.stringData = '' | ||
this.inputBuffer = '' | ||
this.loginPromptReceived = false; | ||
@@ -259,7 +279,11 @@ | ||
else if (this.telnetState === 'response') { | ||
var stringData = chunk.toString() | ||
if (this.inputBuffer.length >= this.maxBufferLength) { | ||
return this.emit('bufferexceeded') | ||
} | ||
this.stringData += stringData | ||
promptIndex = utils.search(this.stringData, this.shellPrompt) | ||
const stringData = chunk.toString() | ||
this.inputBuffer += stringData | ||
promptIndex = utils.search(this.inputBuffer, this.shellPrompt) | ||
if (promptIndex === -1 && stringData.length !== 0) { | ||
@@ -273,21 +297,21 @@ if (utils.search(stringData, this.pageSeparator) !== -1) { | ||
this.cmdOutput = this.stringData.split(this.irs) | ||
this.response = this.inputBuffer.split(this.irs) | ||
for (let i = 0; i < this.cmdOutput.length; i++) { | ||
if (utils.search(this.cmdOutput[i], this.pageSeparator) !== -1) { | ||
this.cmdOutput[i] = this.cmdOutput[i].replace(this.pageSeparator, '') | ||
for (let i = 0; i < this.response.length; i++) { | ||
if (utils.search(this.response[i], this.pageSeparator) !== -1) { | ||
this.response[i] = this.response[i].replace(this.pageSeparator, '') | ||
if (this.cmdOutput[i].length === 0) this.cmdOutput.splice(i, 1) | ||
if (this.response[i].length === 0) this.response.splice(i, 1) | ||
} | ||
} | ||
if (this.echoLines === 1) this.cmdOutput.shift() | ||
else if (this.echoLines > 1) this.cmdOutput.splice(0, this.echoLines) | ||
if (this.echoLines === 1) this.response.shift() | ||
else if (this.echoLines > 1) this.response.splice(0, this.echoLines) | ||
// remove prompt | ||
if (this.stripShellPrompt) { | ||
this.cmdOutput.pop() | ||
this.response.pop() | ||
// add a blank line so that command output | ||
// maintains the trailing new line | ||
this.cmdOutput.push('') | ||
this.response.push('') | ||
} | ||
@@ -294,0 +318,0 @@ |
@@ -8,3 +8,3 @@ { | ||
}, | ||
"version": "0.12.1", | ||
"version": "0.12.2", | ||
"main": "./lib/index.js", | ||
@@ -11,0 +11,0 @@ "engine": "node >= 6.9.1", |
@@ -188,2 +188,3 @@ [![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/mkozjak/node-telnet-client/blob/master/LICENSE) | ||
* `sendTimeout`: A timeout used to wait for a server reply when the 'send' method is used. Defaults to 2000 (ms). | ||
* `maxBufferLength`: Maximum buffer length in bytes which can be filled with response data. Defaults to 1M. | ||
* `debug`: Enable/disable debug logs on console. Defaults to false. | ||
@@ -209,2 +210,3 @@ | ||
* `execTimeout`: A timeout used to wait for a server reply when this method is used. Defaults to 'undefined'. | ||
* `maxBufferLength`: Maximum buffer length in bytes which can be filled with response data. Defaults to 1M. | ||
* `irs`: Input record separator. A separator used to distinguish between lines of the response. Defaults to '\r\n'. | ||
@@ -223,2 +225,3 @@ * `ors`: Output record separator. A separator used to execute commands (break lines on input). Defaults to '\n'. | ||
* `timeout`: A timeout used to wait for a server reply when the 'send' method is used. Defaults to 2000 (ms) or to sendTimeout ('connect' method) if set. | ||
* `maxBufferLength`: Maximum buffer length in bytes which can be filled with response data. Defaults to 1M. | ||
@@ -225,0 +228,0 @@ ### connection.end() -> Promise |
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
134886
15
1102
270
8