You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

expect-telnet

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expect-telnet - npm Package Compare versions

Comparing version

to
0.4.0

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 [![NPM version](https://img.shields.io/npm/v/expect-telnet.svg?style=flat)](https://www.npmjs.org/package/expect-telnet) [![Dependency Status](http://img.shields.io/david/silverwind/expect-telnet.svg?style=flat)](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