expect-telnet
Advanced tools
Comparing version
22
index.js
"use strict"; | ||
var net = require("net"); | ||
var parse = require("url-parse-lax"); | ||
var TIMEOUT = 5000; | ||
function endSocket(socket) { | ||
socket.removeAllListeners("data").removeAllListeners("error").end(); | ||
} | ||
module.exports = function (dest, seq, opts, cb) { | ||
@@ -10,3 +15,3 @@ var socket = new net.Socket(), interacting, saved = ""; | ||
socket.setTimeout(opts.timeout || 3000); | ||
socket.setTimeout(opts.timeout || TIMEOUT); | ||
socket.once("connect", socket.setNoDelay.bind(socket)); | ||
@@ -19,3 +24,3 @@ | ||
if (interacting) interacting = false; | ||
socket.removeAllListeners("data").removeAllListeners("error").end(); | ||
endSocket(socket); | ||
cb(err); | ||
@@ -39,8 +44,16 @@ }); | ||
if (!seq[i] || seq[i].done) { | ||
socket.removeAllListeners("data").removeAllListeners("error").end(); | ||
endSocket(socket); | ||
return cb(); | ||
} | ||
if (!seq[i].timeout) { | ||
seq[i].timeout = setTimeout(function () { | ||
endSocket(socket); | ||
cb(new Error("Expect sequence timeout: " + seq[i].expect)); | ||
}, opts.timeout || TIMEOUT); | ||
} | ||
saved += chunk; | ||
if (saved.indexOf(seq[i].expect) !== -1) { | ||
clearTimeout(seq[i].timeout); | ||
seq[i].done = true; | ||
@@ -50,3 +63,3 @@ | ||
var lines = []; | ||
saved.split(/\r\n/).forEach(function (line) { | ||
saved.split(/\r?\n/).forEach(function (line) { | ||
if (line) lines.push(line); | ||
@@ -72,3 +85,2 @@ }); | ||
} | ||
saved = ""; | ||
@@ -75,0 +87,0 @@ } |
{ | ||
"name": "expect-telnet", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "telnet automation through expect-send sequences, like in Tcl", | ||
@@ -5,0 +5,0 @@ "author": "silverwind <me@silverwind.io> (https://github.com/silverwind)", |
@@ -9,6 +9,6 @@ # expect-telnet [](https://www.npmjs.org/package/expect-telnet) [](https://david-dm.org/silverwind/expect-telnet) | ||
## Examples | ||
### Log the output of a command | ||
```js | ||
var et = require("expect-telnet"); | ||
// connect, log in, run a command and exit after logging its output. | ||
et("1.2.3.4:23", [ | ||
@@ -18,8 +18,13 @@ {expect: "Username", send: "username\r"}, | ||
{expect: "#" , send: "command\r" }, | ||
{expect: "#" , out: console.log, send: "exit\r"} | ||
{expect: "#" , out: function(output) { | ||
console.log(output); | ||
}, send: "exit\r"} | ||
], function(err) { | ||
if (err) console.error(err); | ||
}); | ||
``` | ||
### Start an interactive session | ||
```js | ||
var et = require("expect-telnet"); | ||
// connect, log in and start an interactive session. | ||
et("1.2.3.4:23", [ | ||
@@ -31,3 +36,3 @@ {expect: "Username", send: "username\r"}, | ||
if (err) console.error(err); | ||
} | ||
}); | ||
``` | ||
@@ -49,4 +54,5 @@ | ||
#### Options | ||
- `timeout` *number: Connection timeout in milliseconds. | ||
- `exit` *boolean*: Whether to exit the process when interacting ends. | ||
- `timeout` *number: Timeout for connection and expect sequences in milliseconds. Set to `infinity` for no timeout. | ||
- `exit` *boolean*: Whether to exit the process when interacting ends. | ||
© 2015 [silverwind](https://github.com/silverwind), distributed under BSD licence |
6086
6.31%72
18.03%55
12.24%