-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelnet-client.js
More file actions
156 lines (137 loc) · 4.7 KB
/
telnet-client.js
File metadata and controls
156 lines (137 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict'
let net = require("net");
module.exports = function() {
this.connect = (opts) => {
const host = (typeof opts.host !== 'undefined' ? opts.host : '127.0.0.1')
const port = (typeof opts.port !== 'undefined' ? opts.port : 23)
const timeout = (typeof opts.timeout !== 'undefined' ? opts.timeout : 500)
// Set prompt regex defaults
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)
// username and password
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')
this.ors = (typeof opts.ors !== 'undefined' ? opts.ors : '\n')
this.echoLines = (typeof opts.echoLines !== 'undefined' ? opts.echoLines : 1)
this.stripShellPrompt = (typeof opts.stripShellPrompt !== 'undefined' ? opts.stripShellPrompt : true)
this.execTimeout = (typeof opts.execTimeout !== 'undefined' ? opts.execTimeout : 2000)
this.sendTimeout = (typeof opts.sendTimeout !== 'undefined' ? opts.sendTimeout : 2000)
this.maxBufferLength = (typeof opts.maxBufferLength !== 'undefined' ? opts.maxBufferLength : 1048576)
this.socket = new net.Socket()
this.socket.timeout = timeout;
this.socket.connect(host, port)
let ctimeout = null;
if (timeout) {
ctimeout = setTimeout(() => {
this.socket.close();
}, timeout)
}
while (1) {
let res = _parseData(this.socket.recv())
if (!res) continue
if (ctimeout !== null) {
clearTimeout(ctimeout);
}
if (_search(res, this.loginPrompt) !== -1) {
this.socket.write(new Buffer(this.username + this.ors))
} else if (_search(res, this.passwordPrompt) !== -1) {
this.socket.write(new Buffer(this.password + this.ors))
} else if (_search(res, this.shellPrompt) !== -1) {
return this.shellPrompt;
}
}
}
this.exec = (cmd, opts) => {
if (opts && opts instanceof Object) {
this.shellPrompt = opts.shellPrompt || this.shellPrompt
this.loginPrompt = opts.loginPrompt || this.loginPrompt
this.failedLoginMatch = opts.failedLoginMatch || this.failedLoginMatch
this.timeout = opts.timeout || this.timeout
this.execTimeout = opts.execTimeout || this.execTimeout
this.irs = opts.irs || this.irs
this.ors = opts.ors || this.ors
this.echoLines = opts.echoLines || this.echoLines
this.maxBufferLength = opts.maxBufferLength || this.maxBufferLength
}
cmd += this.ors;
let execTimeout = null;
if (this.execTimeout) {
execTimeout = setTimeout(() => {
this.socket.close();
}, this.execTimeout);
}
this.socket.write(new Buffer(cmd));
let ret = ""
while (1) {
ret += _parseData(this.socket.recv())
if (_search(ret, this.shellPrompt) !== -1) {
let rets = ret.split(this.irs)
if (this.echoLines == 1)
rets.shift()
else if (this.echoLines > 1)
rets.splice(0, this.echoLines)
if (this.stripShellPrompt) {
rets.pop()
}
if (execTimeout !== null) {
clearTimeout(execTimeout)
}
return rets.join(this.irs)
}
}
}
this.send = (data, opts) => {
if (opts && opts instanceof Object) {
this.ors = opts.ors || this.ors
this.sendTimeout = opts.timeout || this.sendTimeout
this.maxBufferLength = opts.maxBufferLength || this.maxBufferLength
data += this.ors
}
this.socket.write(new Buffer(data));
return this.socket.read();
}
this.recv = () => {
return this.socket.recv();
}
this.close = () => {
this.socket.close()
this.socket.dispose()
}
var _parseData = (chunk) => {
if (!chunk) {
throw (new Error("Broken pipe"));
}
if (chunk[0] === 255 && chunk[1] !== 255) {
const negReturn = _negotiate(chunk)
if (negReturn == undefined) return
else chunk = negReturn
}
return chunk.toString();
}
var _negotiate = (chunk) => {
// info: http://tools.ietf.org/html/rfc1143#section-7
// refuse to start performing and ack the start of performance
// DO -> WONT WILL -> DO
const packetLength = chunk.length
let negData = chunk
let cmdData = null
let negResp = null
for (let i = 0; i < packetLength; i += 3) {
if (chunk[i] != 255) {
negData = chunk.slice(0, i)
cmdData = chunk.slice(i)
break
}
}
negResp = negData.toString('hex').replace(/fd/g, 'fc').replace(/fb/g, 'fd')
this.socket.write(new Buffer(negResp, 'hex'));
if (cmdData != undefined) return cmdData
else return
}
var _search = (str, pattern) => {
if (pattern instanceof RegExp) return str.search(pattern)
else return str.indexOf(pattern)
}
}