ssh2shell 2.0.9 → 2.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ssh2shell.js +1024 -1021
- package/package.json +2 -2
- package/src/ssh2shell.coffee +2 -1
- package/Lots of commands.log +0 -445
- package/first.log +0 -30
- package/second.log +0 -30
package/lib/ssh2shell.js
CHANGED
|
@@ -1,1021 +1,1024 @@
|
|
|
1
|
-
// Generated by CoffeeScript 1.12.7
|
|
2
|
-
(function() {
|
|
3
|
-
var SSH2Shell, Stream, typeIsArray,
|
|
4
|
-
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
|
5
|
-
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
6
|
-
hasProp = {}.hasOwnProperty;
|
|
7
|
-
|
|
8
|
-
typeIsArray = Array.isArray || function(value) {
|
|
9
|
-
return {}.toString.call(value) === '[object Array]';
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
Stream = require('stream');
|
|
13
|
-
|
|
14
|
-
SSH2Shell = (function(superClass) {
|
|
15
|
-
extend(SSH2Shell, superClass);
|
|
16
|
-
|
|
17
|
-
SSH2Shell.prototype.sshObj = {};
|
|
18
|
-
|
|
19
|
-
SSH2Shell.prototype.command = "";
|
|
20
|
-
|
|
21
|
-
SSH2Shell.prototype._hosts = [];
|
|
22
|
-
|
|
23
|
-
SSH2Shell.prototype._sshToNextHost = false;
|
|
24
|
-
|
|
25
|
-
SSH2Shell.prototype._primaryhostSessionText = "";
|
|
26
|
-
|
|
27
|
-
SSH2Shell.prototype._allSessions = "";
|
|
28
|
-
|
|
29
|
-
SSH2Shell.prototype._connections = [];
|
|
30
|
-
|
|
31
|
-
SSH2Shell.prototype._stream = {};
|
|
32
|
-
|
|
33
|
-
SSH2Shell.prototype._buffer = "";
|
|
34
|
-
|
|
35
|
-
SSH2Shell.prototype.idleTime = 5000;
|
|
36
|
-
|
|
37
|
-
SSH2Shell.prototype.asciiFilter = "";
|
|
38
|
-
|
|
39
|
-
SSH2Shell.prototype.textColorFilter = "";
|
|
40
|
-
|
|
41
|
-
SSH2Shell.prototype.passwordPrompt = "";
|
|
42
|
-
|
|
43
|
-
SSH2Shell.prototype.passphrasePrompt = "";
|
|
44
|
-
|
|
45
|
-
SSH2Shell.prototype.standardPrompt = "";
|
|
46
|
-
|
|
47
|
-
SSH2Shell.prototype._callback = function() {};
|
|
48
|
-
|
|
49
|
-
SSH2Shell.prototype.onCommandProcessing = function() {};
|
|
50
|
-
|
|
51
|
-
SSH2Shell.prototype.onCommandComplete = function() {};
|
|
52
|
-
|
|
53
|
-
SSH2Shell.prototype.onCommandTimeout = function() {};
|
|
54
|
-
|
|
55
|
-
SSH2Shell.prototype.onEnd = function() {};
|
|
56
|
-
|
|
57
|
-
SSH2Shell.prototype._onData = function(data) {
|
|
58
|
-
this._buffer += data;
|
|
59
|
-
if (this.command.length > 0 && !this.standardPrompt.test(this._sanitizeResponse())) {
|
|
60
|
-
this.emit('commandProcessing', this.command, this._buffer, this.sshObj, this._stream);
|
|
61
|
-
if (this.idleTimer) {
|
|
62
|
-
clearTimeout(this.idleTimer);
|
|
63
|
-
}
|
|
64
|
-
this.idleTimer = setTimeout((function(_this) {
|
|
65
|
-
return function() {
|
|
66
|
-
return _this.emit('commandTimeout', _this.command, _this._buffer, _this._stream, _this.connection);
|
|
67
|
-
};
|
|
68
|
-
})(this), this.idleTime);
|
|
69
|
-
} else if (this.command.length < 1 && !this.standardPrompt.test(this._buffer)) {
|
|
70
|
-
this.emit('commandProcessing', this.command, this._buffer, this.sshObj, this._stream);
|
|
71
|
-
}
|
|
72
|
-
if (this.dataReceivedTimer) {
|
|
73
|
-
clearTimeout(this.dataReceivedTimer);
|
|
74
|
-
}
|
|
75
|
-
return this.dataReceivedTimer = setTimeout((function(_this) {
|
|
76
|
-
return function() {
|
|
77
|
-
if (_this.idleTimer) {
|
|
78
|
-
clearTimeout(_this.idleTimer);
|
|
79
|
-
}
|
|
80
|
-
if (!_this.sshObj.disableColorFilter) {
|
|
81
|
-
_this._buffer = _this._buffer.replace(_this.textColorFilter, "");
|
|
82
|
-
}
|
|
83
|
-
if (!_this.sshObj.disableASCIIFilter) {
|
|
84
|
-
_this._buffer = _this._buffer.replace(_this.asciiFilter, "");
|
|
85
|
-
}
|
|
86
|
-
switch (true) {
|
|
87
|
-
case _this.command.length > 0 && _this.command.indexOf("sudo ") !== -1:
|
|
88
|
-
if (_this.sshObj.debug) {
|
|
89
|
-
_this.emit('msg', _this.sshObj.server.host + ": Sudo command data");
|
|
90
|
-
}
|
|
91
|
-
return _this._processPasswordPrompt();
|
|
92
|
-
case _this.sshObj.sshToNextHost && _this.command.length > 0 && _this.command.indexOf("ssh ") === 0:
|
|
93
|
-
if (_this.sshObj.debug) {
|
|
94
|
-
_this.emit('msg', _this.sshObj.server.host + ": SSH command data");
|
|
95
|
-
}
|
|
96
|
-
return _this._processSSHPrompt();
|
|
97
|
-
case _this.command.length > 0 && _this.standardPrompt.test(_this._sanitizeResponse()):
|
|
98
|
-
if (_this.sshObj.debug) {
|
|
99
|
-
_this.emit('msg', _this.sshObj.server.host + ": Normal prompt detected");
|
|
100
|
-
}
|
|
101
|
-
_this.sshObj.pwSent = false;
|
|
102
|
-
return _this._commandComplete();
|
|
103
|
-
case _this.command.length < 1 && _this.standardPrompt.test(_this._buffer):
|
|
104
|
-
if (_this.sshObj.debug) {
|
|
105
|
-
_this.emit('msg', _this.sshObj.server.host + ": First prompt detected");
|
|
106
|
-
}
|
|
107
|
-
if (_this.sshObj.showBanner) {
|
|
108
|
-
_this.sshObj.sessionText += _this._buffer;
|
|
109
|
-
}
|
|
110
|
-
return _this._nextCommand();
|
|
111
|
-
default:
|
|
112
|
-
if (_this.sshObj.debug) {
|
|
113
|
-
_this.emit('msg', "Data processing: data received timeout");
|
|
114
|
-
}
|
|
115
|
-
return _this.idleTimer = setTimeout(function() {
|
|
116
|
-
return _this.emit('commandTimeout', _this.command, _this._buffer, _this._stream, _this.connection);
|
|
117
|
-
}, _this.idleTime);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
})(this), this.dataIdleTime);
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
SSH2Shell.prototype._sanitizeResponse = function() {
|
|
124
|
-
return this._buffer.replace(this.command.substr(0, this._buffer.length), "");
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
SSH2Shell.prototype._processPasswordPrompt = function() {
|
|
128
|
-
var passwordPrompt, response, standardPrompt;
|
|
129
|
-
response = this._sanitizeResponse().trim();
|
|
130
|
-
passwordPrompt = this.passwordPrompt.test(response);
|
|
131
|
-
standardPrompt = this.standardPrompt.test(response);
|
|
132
|
-
if (this.sshObj.debug) {
|
|
133
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo Password prompt detected: " + passwordPrompt);
|
|
134
|
-
}
|
|
135
|
-
if (this.sshObj.debug) {
|
|
136
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo Password prompt: Password sent: " + this.sshObj.pwSent);
|
|
137
|
-
}
|
|
138
|
-
if (this.sshObj.debug) {
|
|
139
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo Password: " + this.sshObj.server.password);
|
|
140
|
-
}
|
|
141
|
-
if (this.sshObj.verbose) {
|
|
142
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo Response: " + response);
|
|
143
|
-
}
|
|
144
|
-
switch (true) {
|
|
145
|
-
case passwordPrompt && !this.sshObj.server.password:
|
|
146
|
-
if (this.sshObj.debug) {
|
|
147
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo password prompt no password set");
|
|
148
|
-
}
|
|
149
|
-
return this._resetFromSudoError();
|
|
150
|
-
case passwordPrompt && !this.sshObj.pwSent:
|
|
151
|
-
if (this.sshObj.verbose) {
|
|
152
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo password prompt: Buffer: " + response);
|
|
153
|
-
}
|
|
154
|
-
if (this.sshObj.debug) {
|
|
155
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo password prompt: Send password ");
|
|
156
|
-
}
|
|
157
|
-
if (this.sshObj.verbose) {
|
|
158
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo sent password: " + this.sshObj.server.password);
|
|
159
|
-
}
|
|
160
|
-
this.sshObj.pwSent = true;
|
|
161
|
-
return this._runCommand("" + this.sshObj.server.password);
|
|
162
|
-
case passwordPrompt && this.sshObj.pwSent:
|
|
163
|
-
if (this.sshObj.verbose) {
|
|
164
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo password faied: response: " + response);
|
|
165
|
-
}
|
|
166
|
-
if (this.sshObj.debug) {
|
|
167
|
-
this.emit('error', this.sshObj.server.host + ": Sudo password was incorrect for " + this.sshObj.server.userName + ", Sudo authentication");
|
|
168
|
-
}
|
|
169
|
-
if (this.sshObj.debug) {
|
|
170
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo failed password prompt: Password: [" + this.sshObj.server.password + "]");
|
|
171
|
-
}
|
|
172
|
-
this.sshObj.pwSent = false;
|
|
173
|
-
return this._resetFromSudoError();
|
|
174
|
-
case standardPrompt:
|
|
175
|
-
if (this.sshObj.debug) {
|
|
176
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo password accepted");
|
|
177
|
-
}
|
|
178
|
-
this.sshObj.pwSent = false;
|
|
179
|
-
if (this.sshObj.verbose) {
|
|
180
|
-
this.emit('msg', this.sshObj.server.host + ": Sudo Standard Response: " + response);
|
|
181
|
-
}
|
|
182
|
-
return this._commandComplete();
|
|
183
|
-
default:
|
|
184
|
-
return this.idleTimer = setTimeout((function(_this) {
|
|
185
|
-
return function() {
|
|
186
|
-
return _this.emit('commandTimeout', _this.command, response, _this._stream, _this._connection);
|
|
187
|
-
};
|
|
188
|
-
})(this), this.idleTime);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
SSH2Shell.prototype._resetFromSudoError = function() {
|
|
193
|
-
this.sshObj.pwSent = false;
|
|
194
|
-
this.sshObj.sessionText += "" + this._buffer;
|
|
195
|
-
this._buffer = "";
|
|
196
|
-
this.command = "";
|
|
197
|
-
return this._stream.write('\x03');
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
SSH2Shell.prototype._processSSHPrompt = function() {
|
|
201
|
-
var passphrasePrompt, passwordPrompt, response, standardPrompt, using;
|
|
202
|
-
response = this._sanitizeResponse().trim();
|
|
203
|
-
if (this.idleTimer) {
|
|
204
|
-
clearTimeout(this.idleTimer);
|
|
205
|
-
}
|
|
206
|
-
passwordPrompt = this.passwordPrompt.test(response) && !this.sshObj.server.hasOwnProperty("passPhrase");
|
|
207
|
-
passphrasePrompt = this.passphrasePrompt.test(response) && this.sshObj.server.hasOwnProperty("passPhrase");
|
|
208
|
-
standardPrompt = this.standardPrompt.test(response);
|
|
209
|
-
if (this.sshObj.verbose) {
|
|
210
|
-
this.emit('msg', this.sshObj.server.host + ": SSH: Password previously sent: " + this.sshObj.sshAuth);
|
|
211
|
-
}
|
|
212
|
-
if (!this.sshObj.sshAuth) {
|
|
213
|
-
if (this.sshObj.debug) {
|
|
214
|
-
this.emit('msg', this.sshObj.server.host + ": First SSH prompt detection");
|
|
215
|
-
}
|
|
216
|
-
if (passwordPrompt) {
|
|
217
|
-
if (this.sshObj.debug) {
|
|
218
|
-
this.emit('msg', this.sshObj.server.host + ": SSH send password");
|
|
219
|
-
}
|
|
220
|
-
this.sshObj.sshAuth = true;
|
|
221
|
-
this._buffer = "";
|
|
222
|
-
return this._runCommand("" + this.sshObj.server.password);
|
|
223
|
-
} else if (passphrasePrompt) {
|
|
224
|
-
if (this.sshObj.debug) {
|
|
225
|
-
this.emit('msg', this.sshObj.server.host + ": SSH send passphrase");
|
|
226
|
-
}
|
|
227
|
-
this.sshObj.sshAuth = true;
|
|
228
|
-
this._buffer = "";
|
|
229
|
-
return this._runCommand("" + this.sshObj.server.passPhrase);
|
|
230
|
-
} else if (standardPrompt) {
|
|
231
|
-
if (this.sshObj.debug) {
|
|
232
|
-
this.emit('msg', this.sshObj.server.host + ": SSH standard prompt: connection failed");
|
|
233
|
-
}
|
|
234
|
-
this.emit('msg', this.sshObj.server.host + ": SSH connection failed");
|
|
235
|
-
this.emit('msg', this.sshObj.server.host + ": SSH failed response: " + response);
|
|
236
|
-
this.sshObj.sessionText += this.sshObj.server.host + ": SSH failed: response: " + response;
|
|
237
|
-
return this._runExit();
|
|
238
|
-
} else {
|
|
239
|
-
this.emit('msg', this.sshObj.server.host + ": SSH no prompt was not detected");
|
|
240
|
-
if (this.sshObj.verbose) {
|
|
241
|
-
this.emit('msg', this.sshObj.server.host + ": SSH response: " + response);
|
|
242
|
-
}
|
|
243
|
-
if (this.sshObj.verbose && !this.sshObj.server.hasOwnProperty("password")) {
|
|
244
|
-
this.emit('msg', this.sshObj.server.host + ": SSH password prompt: " + this.sshObj.passwordPrompt);
|
|
245
|
-
}
|
|
246
|
-
if (this.sshObj.verbose && this.sshObj.server.hasOwnProperty("passPhrase")) {
|
|
247
|
-
this.emit('msg', this.sshObj.server.host + ": SSH passphrase prompt: " + this.sshObj.passphrasePrompt);
|
|
248
|
-
}
|
|
249
|
-
if (this.sshObj.verbose) {
|
|
250
|
-
this.emit('msg', this.sshObj.server.host + ": SSH standard prompt: " + this.sshObj.standardPrompt);
|
|
251
|
-
}
|
|
252
|
-
return this._runExit();
|
|
253
|
-
}
|
|
254
|
-
} else {
|
|
255
|
-
if (this.sshObj.debug) {
|
|
256
|
-
this.emit('msg', this.sshObj.server.host + ": SSH post authentication prompt detection");
|
|
257
|
-
}
|
|
258
|
-
if (standardPrompt) {
|
|
259
|
-
if (this.sshObj.debug) {
|
|
260
|
-
this.emit('msg', this.sshObj.server.host + ": SSH complete: normal prompt");
|
|
261
|
-
}
|
|
262
|
-
if (this.sshObj.showBanner) {
|
|
263
|
-
this.sshObj.sessionText += "\n " + response;
|
|
264
|
-
}
|
|
265
|
-
this.sshObj.exitCommands.push("exit");
|
|
266
|
-
this.emit('msg', "" + this.sshObj.connectedMessage);
|
|
267
|
-
this.emit('msg', "" + this.sshObj.readyMessage);
|
|
268
|
-
if (this.sshObj.debug) {
|
|
269
|
-
this.emit('msg', this.sshObj.server.host + ": SSH complete: next command");
|
|
270
|
-
}
|
|
271
|
-
this.sshObj.sshToNextHost = false;
|
|
272
|
-
return this._nextCommand();
|
|
273
|
-
} else if (passwordPrompt || passphrasePrompt) {
|
|
274
|
-
this.emit('msg', this.sshObj.server.host + ": SSH authentication failed");
|
|
275
|
-
if (this.sshObj.debug) {
|
|
276
|
-
this.emit('msg', this.sshObj.server.host + ": SSH auth failed");
|
|
277
|
-
}
|
|
278
|
-
if (this.sshObj.verbose) {
|
|
279
|
-
this.emit('msg', this.sshObj.server.host + ": SSH: failed response: " + response);
|
|
280
|
-
}
|
|
281
|
-
this.sshObj.sshAuth = false;
|
|
282
|
-
using = (function() {
|
|
283
|
-
switch (false) {
|
|
284
|
-
case !passwordPrompt:
|
|
285
|
-
return "SSH password: " + this.sshObj.server.password;
|
|
286
|
-
case !passphrasePrompt:
|
|
287
|
-
return "SSH passphrase: " + this.sshObj.server.passPhrase;
|
|
288
|
-
}
|
|
289
|
-
}).call(this);
|
|
290
|
-
this.emit('error', this.sshObj.server.host + ": SSH authentication failed for " + this.sshObj.server.userName + "@" + this.sshObj.server.host, "Nested host authentication");
|
|
291
|
-
if (this.sshObj.debug) {
|
|
292
|
-
this.emit('msg', (this.sshObj.server.host + ": SSH auth failed: Using ") + using);
|
|
293
|
-
}
|
|
294
|
-
if (this.sshObj.verbose) {
|
|
295
|
-
this.emit('msg', this.sshObj.server.host + ": SSH response: " + response);
|
|
296
|
-
}
|
|
297
|
-
if (this._connections.length > 0) {
|
|
298
|
-
return this._previousHost();
|
|
299
|
-
}
|
|
300
|
-
return this._runExit();
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
SSH2Shell.prototype._processNotifications = function() {
|
|
306
|
-
var msgNote, sessionNote;
|
|
307
|
-
if (this.command) {
|
|
308
|
-
if ((sessionNote = this.command.match(/^`(.*)`$/))) {
|
|
309
|
-
if (this.sshObj.debug) {
|
|
310
|
-
this.emit('msg', this.sshObj.server.host + ": Notifications: sessionText output");
|
|
311
|
-
}
|
|
312
|
-
if (this._connections.length > 0) {
|
|
313
|
-
this.sshObj.sessionText += this.sshObj.server.host + ": Note: " + sessionNote[1] + this.sshObj.enter;
|
|
314
|
-
} else {
|
|
315
|
-
this.sshObj.sessionText += "Note: " + sessionNote[1] + this.sshObj.enter;
|
|
316
|
-
}
|
|
317
|
-
if (this.sshObj.verbose) {
|
|
318
|
-
this.emit('msg', sessionNote[1]);
|
|
319
|
-
}
|
|
320
|
-
return this._nextCommand();
|
|
321
|
-
} else if ((msgNote = this.command.match(/^msg:(.*)$/))) {
|
|
322
|
-
if (this.sshObj.debug) {
|
|
323
|
-
this.emit('msg', this.sshObj.server.host + ": Notifications: msg to output");
|
|
324
|
-
}
|
|
325
|
-
this.emit('msg', this.sshObj.server.host + ": Note: " + msgNote[1]);
|
|
326
|
-
return this._nextCommand();
|
|
327
|
-
} else {
|
|
328
|
-
if (this.sshObj.debug) {
|
|
329
|
-
this.emit('msg', this.sshObj.server.host + ": Notifications: Normal Command to run");
|
|
330
|
-
}
|
|
331
|
-
return this._checkCommand();
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
SSH2Shell.prototype._commandComplete = function() {
|
|
337
|
-
var response;
|
|
338
|
-
response = this._buffer.trim();
|
|
339
|
-
if (this.command !== "" && this.command !== "exit" && this.sshObj.sshToNextHost !== true) {
|
|
340
|
-
if (this.sshObj.verbose) {
|
|
341
|
-
this.emit('msg', this.sshObj.server.host + ": Command complete:\nCommand:\n " + this.command + "\nResponse: " + response);
|
|
342
|
-
}
|
|
343
|
-
this.sshObj.sessionText += response;
|
|
344
|
-
if (this.sshObj.debug) {
|
|
345
|
-
this.emit('msg', this.sshObj.server.host + ": Raising commandComplete event");
|
|
346
|
-
}
|
|
347
|
-
this.emit('commandComplete', this.command, this._buffer, this.sshObj);
|
|
348
|
-
}
|
|
349
|
-
return this._nextCommand();
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
SSH2Shell.prototype._nextCommand = function() {
|
|
353
|
-
this._buffer = "";
|
|
354
|
-
if (this.sshObj.commands.length > 0) {
|
|
355
|
-
if (this.sshObj.verbose) {
|
|
356
|
-
this.emit('msg', this.sshObj.server.host + ": Host.commands: " + this.sshObj.commands);
|
|
357
|
-
}
|
|
358
|
-
this.command = this.sshObj.commands.shift();
|
|
359
|
-
if (this.sshObj.verbose) {
|
|
360
|
-
this.emit('msg', this.sshObj.server.host + ": Next command from host.commands: " + this.command);
|
|
361
|
-
}
|
|
362
|
-
return this._processNotifications();
|
|
363
|
-
} else {
|
|
364
|
-
if (this.sshObj.debug) {
|
|
365
|
-
this.emit('msg', this.sshObj.server.host + ": No commands so exit");
|
|
366
|
-
}
|
|
367
|
-
return this._runExit();
|
|
368
|
-
}
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
SSH2Shell.prototype._checkCommand = function() {
|
|
372
|
-
if (this.command !== "") {
|
|
373
|
-
return this._runCommand(this.command);
|
|
374
|
-
} else {
|
|
375
|
-
if (this.sshObj.debug) {
|
|
376
|
-
this.emit('msg', this.sshObj.server.host + ": No command so exit");
|
|
377
|
-
}
|
|
378
|
-
return this._runExit();
|
|
379
|
-
}
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
SSH2Shell.prototype._runCommand = function(command) {
|
|
383
|
-
if (this.sshObj.verbose) {
|
|
384
|
-
this.emit('msg', this.sshObj.server.host + ": sending: " + command);
|
|
385
|
-
}
|
|
386
|
-
if (this.sshObj.debug) {
|
|
387
|
-
this.emit('msg', this.sshObj.server.host + ": run command");
|
|
388
|
-
}
|
|
389
|
-
return this._stream.write("" + command + this.sshObj.enter);
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
SSH2Shell.prototype._previousHost = function() {
|
|
393
|
-
if (this.sshObj.debug) {
|
|
394
|
-
this.emit('msg', this.sshObj.server.host + ": Load previous host config");
|
|
395
|
-
}
|
|
396
|
-
this.emit('end', this.sshObj.server.host + ": \n" + this.sshObj.sessionText, this.sshObj);
|
|
397
|
-
if (this.sshObj.debug) {
|
|
398
|
-
this.emit('msg', this.sshObj.server.host + ": Previous hosts: " + this._connections.length);
|
|
399
|
-
}
|
|
400
|
-
if (this._connections.length > 0) {
|
|
401
|
-
this.sshObj = this._connections.pop();
|
|
402
|
-
if (this.sshObj.debug) {
|
|
403
|
-
this.emit('msg', this.sshObj.server.host + ": Reload previous host");
|
|
404
|
-
}
|
|
405
|
-
return this._runExit();
|
|
406
|
-
} else {
|
|
407
|
-
return this._runExit();
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
SSH2Shell.prototype._nextHost = function() {
|
|
412
|
-
var nextHost;
|
|
413
|
-
nextHost = this.sshObj.hosts.shift();
|
|
414
|
-
if (this.sshObj.debug) {
|
|
415
|
-
this.emit('msg', this.sshObj.server.host + ": SSH to " + nextHost.server.host);
|
|
416
|
-
}
|
|
417
|
-
if (this.sshObj.debug) {
|
|
418
|
-
this.emit('msg', this.sshObj.server.host + ": Clearing previous event handlers");
|
|
419
|
-
}
|
|
420
|
-
if (this.sshObj.debug) {
|
|
421
|
-
this.emit('msg', this.sshObj.server.host + ": Remove previous event handlers");
|
|
422
|
-
}
|
|
423
|
-
this._removeListeners();
|
|
424
|
-
this._connections.push(this.sshObj);
|
|
425
|
-
this.sshObj = nextHost;
|
|
426
|
-
return this._initiate(this._sshConnect);
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
SSH2Shell.prototype._nextPrimaryHost = function(connect) {
|
|
430
|
-
if (this._hosts.length > 0) {
|
|
431
|
-
if (this.sshObj.server) {
|
|
432
|
-
if (this.sshObj.debug) {
|
|
433
|
-
this.emit('msg', this.sshObj.server.host + ": Current primary host");
|
|
434
|
-
}
|
|
435
|
-
if (this.sshObj.debug) {
|
|
436
|
-
this.emit('msg', this.sshObj.server.host + ": Remove previous event handlers");
|
|
437
|
-
}
|
|
438
|
-
this.emit('end', this._primaryhostSessionText, this.sshObj);
|
|
439
|
-
this._removeListeners();
|
|
440
|
-
}
|
|
441
|
-
this.sshObj = this._hosts.shift();
|
|
442
|
-
this._primaryhostSessionText = this.sshObj.server.host + ": ";
|
|
443
|
-
if (this.sshObj.debug) {
|
|
444
|
-
this.emit('msg', this.sshObj.server.host + ": Next primary host");
|
|
445
|
-
}
|
|
446
|
-
return this._initiate(connect);
|
|
447
|
-
} else {
|
|
448
|
-
if (this.sshObj.debug) {
|
|
449
|
-
this.emit('msg', this.sshObj.server.host + ": No more primary hosts");
|
|
450
|
-
}
|
|
451
|
-
return this._runExit;
|
|
452
|
-
}
|
|
453
|
-
};
|
|
454
|
-
|
|
455
|
-
SSH2Shell.prototype._sshConnect = function() {
|
|
456
|
-
var option, ref, sshFlags, sshOptions, value;
|
|
457
|
-
sshFlags = "-x";
|
|
458
|
-
sshOptions = "";
|
|
459
|
-
if (this.sshObj.server.ssh) {
|
|
460
|
-
if (this.sshObj.server.ssh.forceProtocolVersion) {
|
|
461
|
-
sshFlags += this.sshObj.server.ssh.forceProtocolVersion;
|
|
462
|
-
}
|
|
463
|
-
if (this.sshObj.server.ssh.forceAddressType) {
|
|
464
|
-
sshFlags += this.sshObj.server.ssh.forceAddressType;
|
|
465
|
-
}
|
|
466
|
-
if (this.sshObj.server.ssh.disablePseudoTTY) {
|
|
467
|
-
sshFlags += "T";
|
|
468
|
-
}
|
|
469
|
-
if (this.sshObj.server.ssh.forcePseudoTTY) {
|
|
470
|
-
sshFlags += "t";
|
|
471
|
-
}
|
|
472
|
-
if (this.sshObj.server.ssh.verbose) {
|
|
473
|
-
sshFlags += "v";
|
|
474
|
-
}
|
|
475
|
-
if (this.sshObj.server.ssh.cipherSpec) {
|
|
476
|
-
sshOptions += " -c " + this.sshObj.server.ssh.cipherSpec;
|
|
477
|
-
}
|
|
478
|
-
if (this.sshObj.server.ssh.escape) {
|
|
479
|
-
sshOptions += " -e " + this.sshObj.server.ssh.escape;
|
|
480
|
-
}
|
|
481
|
-
if (this.sshObj.server.ssh.logFile) {
|
|
482
|
-
sshOptions += " -E " + this.sshObj.server.ssh.logFile;
|
|
483
|
-
}
|
|
484
|
-
if (this.sshObj.server.ssh.configFile) {
|
|
485
|
-
sshOptions += " -F " + this.sshObj.server.ssh.configFile;
|
|
486
|
-
}
|
|
487
|
-
if (this.sshObj.server.ssh.identityFile) {
|
|
488
|
-
sshOptions += " -i " + this.sshObj.server.ssh.identityFile;
|
|
489
|
-
}
|
|
490
|
-
if (this.sshObj.server.ssh.loginName) {
|
|
491
|
-
sshOptions += " -l " + this.sshObj.server.ssh.loginName;
|
|
492
|
-
}
|
|
493
|
-
if (this.sshObj.server.ssh.macSpec) {
|
|
494
|
-
sshOptions += " -m " + this.sshObj.server.ssh.macSpec;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
if (this.sshObj.
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
@.emit 'msg', "
|
|
565
|
-
@.emit 'msg', "
|
|
566
|
-
@.emit 'msg', "
|
|
567
|
-
@.emit 'msg', "
|
|
568
|
-
@.emit 'msg', "
|
|
569
|
-
@.emit 'msg', "
|
|
570
|
-
@.emit 'msg', "
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
if (typeof this.sshObj.
|
|
576
|
-
this.removeListener("
|
|
577
|
-
}
|
|
578
|
-
if (typeof this.sshObj.
|
|
579
|
-
this.removeListener("
|
|
580
|
-
}
|
|
581
|
-
if (typeof this.sshObj.
|
|
582
|
-
this.removeListener("
|
|
583
|
-
}
|
|
584
|
-
if (typeof this.sshObj.
|
|
585
|
-
this.removeListener("
|
|
586
|
-
}
|
|
587
|
-
if (typeof this.sshObj.
|
|
588
|
-
this.removeListener("
|
|
589
|
-
}
|
|
590
|
-
if (typeof this.sshObj.
|
|
591
|
-
this.removeListener("
|
|
592
|
-
}
|
|
593
|
-
if (typeof this.sshObj.
|
|
594
|
-
this.removeListener("
|
|
595
|
-
}
|
|
596
|
-
if (typeof this.sshObj.
|
|
597
|
-
this.removeListener("
|
|
598
|
-
}
|
|
599
|
-
if (typeof this.sshObj.
|
|
600
|
-
this.removeListener("
|
|
601
|
-
}
|
|
602
|
-
if (this.
|
|
603
|
-
|
|
604
|
-
}
|
|
605
|
-
if (this.
|
|
606
|
-
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
this.
|
|
615
|
-
this.
|
|
616
|
-
this.
|
|
617
|
-
this.
|
|
618
|
-
this.
|
|
619
|
-
this.
|
|
620
|
-
this.
|
|
621
|
-
this.
|
|
622
|
-
this.
|
|
623
|
-
this.
|
|
624
|
-
this.
|
|
625
|
-
this.
|
|
626
|
-
this.
|
|
627
|
-
this.
|
|
628
|
-
this.
|
|
629
|
-
this.
|
|
630
|
-
this.
|
|
631
|
-
this.
|
|
632
|
-
this.
|
|
633
|
-
this.
|
|
634
|
-
this.
|
|
635
|
-
this.
|
|
636
|
-
this.
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
if (
|
|
666
|
-
|
|
667
|
-
}
|
|
668
|
-
if (
|
|
669
|
-
_this.emit('msg', "
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
if (_this.sshObj.
|
|
686
|
-
_this.emit('msg', "
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
this.
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
if (this.
|
|
713
|
-
this.
|
|
714
|
-
}
|
|
715
|
-
if (
|
|
716
|
-
this.
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
}
|
|
727
|
-
this.
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
if (!this.sshObj.
|
|
734
|
-
this.sshObj.
|
|
735
|
-
}
|
|
736
|
-
if (!this.sshObj.
|
|
737
|
-
this.sshObj.
|
|
738
|
-
}
|
|
739
|
-
if (!this.sshObj.
|
|
740
|
-
this.sshObj.
|
|
741
|
-
}
|
|
742
|
-
if (!this.sshObj.
|
|
743
|
-
this.sshObj.
|
|
744
|
-
}
|
|
745
|
-
if (!this.sshObj.
|
|
746
|
-
this.sshObj.
|
|
747
|
-
}
|
|
748
|
-
if (!this.sshObj.
|
|
749
|
-
this.sshObj.
|
|
750
|
-
}
|
|
751
|
-
if (!this.sshObj.
|
|
752
|
-
this.sshObj.
|
|
753
|
-
}
|
|
754
|
-
if (!this.sshObj.
|
|
755
|
-
this.sshObj.
|
|
756
|
-
}
|
|
757
|
-
if (!this.sshObj.
|
|
758
|
-
this.sshObj.
|
|
759
|
-
}
|
|
760
|
-
if (!this.sshObj.
|
|
761
|
-
this.sshObj.
|
|
762
|
-
}
|
|
763
|
-
if (!this.sshObj.
|
|
764
|
-
this.sshObj.
|
|
765
|
-
}
|
|
766
|
-
if (!this.sshObj.
|
|
767
|
-
this.sshObj.
|
|
768
|
-
}
|
|
769
|
-
if (!this.sshObj.
|
|
770
|
-
this.sshObj.
|
|
771
|
-
}
|
|
772
|
-
if (!this.sshObj.
|
|
773
|
-
this.sshObj.
|
|
774
|
-
}
|
|
775
|
-
if (!this.sshObj.
|
|
776
|
-
this.sshObj.
|
|
777
|
-
}
|
|
778
|
-
if (this.sshObj.
|
|
779
|
-
this.sshObj.
|
|
780
|
-
}
|
|
781
|
-
if (this.sshObj.
|
|
782
|
-
this.sshObj.
|
|
783
|
-
}
|
|
784
|
-
if (
|
|
785
|
-
this.sshObj.
|
|
786
|
-
}
|
|
787
|
-
if (!this.sshObj.
|
|
788
|
-
this.sshObj.
|
|
789
|
-
}
|
|
790
|
-
this.sshObj.
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
if (!this.sshObj.
|
|
801
|
-
this.sshObj.
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
if (!this.
|
|
809
|
-
this.
|
|
810
|
-
}
|
|
811
|
-
if (!this.
|
|
812
|
-
this.
|
|
813
|
-
}
|
|
814
|
-
if (!this.
|
|
815
|
-
this.
|
|
816
|
-
}
|
|
817
|
-
if (!this.
|
|
818
|
-
this.
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
}
|
|
823
|
-
this.sshObj.
|
|
824
|
-
return function(command, response, sshObj) {
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (_this.sshObj.
|
|
837
|
-
_this.emit('msg', _this.sshObj.server.host + ":
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
}
|
|
846
|
-
if (typeof this.sshObj.
|
|
847
|
-
this.on("
|
|
848
|
-
}
|
|
849
|
-
if (typeof this.sshObj.
|
|
850
|
-
this.on("
|
|
851
|
-
}
|
|
852
|
-
if (typeof this.sshObj.
|
|
853
|
-
this.on("
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
if (this.sshObj.
|
|
862
|
-
this.
|
|
863
|
-
}
|
|
864
|
-
if (this.sshObj.
|
|
865
|
-
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
if (_this.
|
|
969
|
-
|
|
970
|
-
}
|
|
971
|
-
if (_this.
|
|
972
|
-
clearTimeout(_this.
|
|
973
|
-
}
|
|
974
|
-
if (
|
|
975
|
-
_this.
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
if (!this.sshObj.server.
|
|
996
|
-
this.sshObj.server.
|
|
997
|
-
}
|
|
998
|
-
if (!this.sshObj.server.
|
|
999
|
-
this.sshObj.server.
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
this.
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1
|
+
// Generated by CoffeeScript 1.12.7
|
|
2
|
+
(function() {
|
|
3
|
+
var SSH2Shell, Stream, typeIsArray,
|
|
4
|
+
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
|
5
|
+
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
6
|
+
hasProp = {}.hasOwnProperty;
|
|
7
|
+
|
|
8
|
+
typeIsArray = Array.isArray || function(value) {
|
|
9
|
+
return {}.toString.call(value) === '[object Array]';
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
Stream = require('stream');
|
|
13
|
+
|
|
14
|
+
SSH2Shell = (function(superClass) {
|
|
15
|
+
extend(SSH2Shell, superClass);
|
|
16
|
+
|
|
17
|
+
SSH2Shell.prototype.sshObj = {};
|
|
18
|
+
|
|
19
|
+
SSH2Shell.prototype.command = "";
|
|
20
|
+
|
|
21
|
+
SSH2Shell.prototype._hosts = [];
|
|
22
|
+
|
|
23
|
+
SSH2Shell.prototype._sshToNextHost = false;
|
|
24
|
+
|
|
25
|
+
SSH2Shell.prototype._primaryhostSessionText = "";
|
|
26
|
+
|
|
27
|
+
SSH2Shell.prototype._allSessions = "";
|
|
28
|
+
|
|
29
|
+
SSH2Shell.prototype._connections = [];
|
|
30
|
+
|
|
31
|
+
SSH2Shell.prototype._stream = {};
|
|
32
|
+
|
|
33
|
+
SSH2Shell.prototype._buffer = "";
|
|
34
|
+
|
|
35
|
+
SSH2Shell.prototype.idleTime = 5000;
|
|
36
|
+
|
|
37
|
+
SSH2Shell.prototype.asciiFilter = "";
|
|
38
|
+
|
|
39
|
+
SSH2Shell.prototype.textColorFilter = "";
|
|
40
|
+
|
|
41
|
+
SSH2Shell.prototype.passwordPrompt = "";
|
|
42
|
+
|
|
43
|
+
SSH2Shell.prototype.passphrasePrompt = "";
|
|
44
|
+
|
|
45
|
+
SSH2Shell.prototype.standardPrompt = "";
|
|
46
|
+
|
|
47
|
+
SSH2Shell.prototype._callback = function() {};
|
|
48
|
+
|
|
49
|
+
SSH2Shell.prototype.onCommandProcessing = function() {};
|
|
50
|
+
|
|
51
|
+
SSH2Shell.prototype.onCommandComplete = function() {};
|
|
52
|
+
|
|
53
|
+
SSH2Shell.prototype.onCommandTimeout = function() {};
|
|
54
|
+
|
|
55
|
+
SSH2Shell.prototype.onEnd = function() {};
|
|
56
|
+
|
|
57
|
+
SSH2Shell.prototype._onData = function(data) {
|
|
58
|
+
this._buffer += data;
|
|
59
|
+
if (this.command.length > 0 && !this.standardPrompt.test(this._sanitizeResponse())) {
|
|
60
|
+
this.emit('commandProcessing', this.command, this._buffer, this.sshObj, this._stream);
|
|
61
|
+
if (this.idleTimer) {
|
|
62
|
+
clearTimeout(this.idleTimer);
|
|
63
|
+
}
|
|
64
|
+
this.idleTimer = setTimeout((function(_this) {
|
|
65
|
+
return function() {
|
|
66
|
+
return _this.emit('commandTimeout', _this.command, _this._buffer, _this._stream, _this.connection);
|
|
67
|
+
};
|
|
68
|
+
})(this), this.idleTime);
|
|
69
|
+
} else if (this.command.length < 1 && !this.standardPrompt.test(this._buffer)) {
|
|
70
|
+
this.emit('commandProcessing', this.command, this._buffer, this.sshObj, this._stream);
|
|
71
|
+
}
|
|
72
|
+
if (this.dataReceivedTimer) {
|
|
73
|
+
clearTimeout(this.dataReceivedTimer);
|
|
74
|
+
}
|
|
75
|
+
return this.dataReceivedTimer = setTimeout((function(_this) {
|
|
76
|
+
return function() {
|
|
77
|
+
if (_this.idleTimer) {
|
|
78
|
+
clearTimeout(_this.idleTimer);
|
|
79
|
+
}
|
|
80
|
+
if (!_this.sshObj.disableColorFilter) {
|
|
81
|
+
_this._buffer = _this._buffer.replace(_this.textColorFilter, "");
|
|
82
|
+
}
|
|
83
|
+
if (!_this.sshObj.disableASCIIFilter) {
|
|
84
|
+
_this._buffer = _this._buffer.replace(_this.asciiFilter, "");
|
|
85
|
+
}
|
|
86
|
+
switch (true) {
|
|
87
|
+
case _this.command.length > 0 && _this.command.indexOf("sudo ") !== -1:
|
|
88
|
+
if (_this.sshObj.debug) {
|
|
89
|
+
_this.emit('msg', _this.sshObj.server.host + ": Sudo command data");
|
|
90
|
+
}
|
|
91
|
+
return _this._processPasswordPrompt();
|
|
92
|
+
case _this.sshObj.sshToNextHost && _this.command.length > 0 && _this.command.indexOf("ssh ") === 0:
|
|
93
|
+
if (_this.sshObj.debug) {
|
|
94
|
+
_this.emit('msg', _this.sshObj.server.host + ": SSH command data");
|
|
95
|
+
}
|
|
96
|
+
return _this._processSSHPrompt();
|
|
97
|
+
case _this.command.length > 0 && _this.standardPrompt.test(_this._sanitizeResponse()):
|
|
98
|
+
if (_this.sshObj.debug) {
|
|
99
|
+
_this.emit('msg', _this.sshObj.server.host + ": Normal prompt detected");
|
|
100
|
+
}
|
|
101
|
+
_this.sshObj.pwSent = false;
|
|
102
|
+
return _this._commandComplete();
|
|
103
|
+
case _this.command.length < 1 && _this.standardPrompt.test(_this._buffer):
|
|
104
|
+
if (_this.sshObj.debug) {
|
|
105
|
+
_this.emit('msg', _this.sshObj.server.host + ": First prompt detected");
|
|
106
|
+
}
|
|
107
|
+
if (_this.sshObj.showBanner) {
|
|
108
|
+
_this.sshObj.sessionText += _this._buffer;
|
|
109
|
+
}
|
|
110
|
+
return _this._nextCommand();
|
|
111
|
+
default:
|
|
112
|
+
if (_this.sshObj.debug) {
|
|
113
|
+
_this.emit('msg', "Data processing: data received timeout");
|
|
114
|
+
}
|
|
115
|
+
return _this.idleTimer = setTimeout(function() {
|
|
116
|
+
return _this.emit('commandTimeout', _this.command, _this._buffer, _this._stream, _this.connection);
|
|
117
|
+
}, _this.idleTime);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
})(this), this.dataIdleTime);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
SSH2Shell.prototype._sanitizeResponse = function() {
|
|
124
|
+
return this._buffer.replace(this.command.substr(0, this._buffer.length), "");
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
SSH2Shell.prototype._processPasswordPrompt = function() {
|
|
128
|
+
var passwordPrompt, response, standardPrompt;
|
|
129
|
+
response = this._sanitizeResponse().trim();
|
|
130
|
+
passwordPrompt = this.passwordPrompt.test(response);
|
|
131
|
+
standardPrompt = this.standardPrompt.test(response);
|
|
132
|
+
if (this.sshObj.debug) {
|
|
133
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo Password prompt detected: " + passwordPrompt);
|
|
134
|
+
}
|
|
135
|
+
if (this.sshObj.debug) {
|
|
136
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo Password prompt: Password sent: " + this.sshObj.pwSent);
|
|
137
|
+
}
|
|
138
|
+
if (this.sshObj.debug) {
|
|
139
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo Password: " + this.sshObj.server.password);
|
|
140
|
+
}
|
|
141
|
+
if (this.sshObj.verbose) {
|
|
142
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo Response: " + response);
|
|
143
|
+
}
|
|
144
|
+
switch (true) {
|
|
145
|
+
case passwordPrompt && !this.sshObj.server.password:
|
|
146
|
+
if (this.sshObj.debug) {
|
|
147
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo password prompt no password set");
|
|
148
|
+
}
|
|
149
|
+
return this._resetFromSudoError();
|
|
150
|
+
case passwordPrompt && !this.sshObj.pwSent:
|
|
151
|
+
if (this.sshObj.verbose) {
|
|
152
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo password prompt: Buffer: " + response);
|
|
153
|
+
}
|
|
154
|
+
if (this.sshObj.debug) {
|
|
155
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo password prompt: Send password ");
|
|
156
|
+
}
|
|
157
|
+
if (this.sshObj.verbose) {
|
|
158
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo sent password: " + this.sshObj.server.password);
|
|
159
|
+
}
|
|
160
|
+
this.sshObj.pwSent = true;
|
|
161
|
+
return this._runCommand("" + this.sshObj.server.password);
|
|
162
|
+
case passwordPrompt && this.sshObj.pwSent:
|
|
163
|
+
if (this.sshObj.verbose) {
|
|
164
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo password faied: response: " + response);
|
|
165
|
+
}
|
|
166
|
+
if (this.sshObj.debug) {
|
|
167
|
+
this.emit('error', this.sshObj.server.host + ": Sudo password was incorrect for " + this.sshObj.server.userName + ", Sudo authentication");
|
|
168
|
+
}
|
|
169
|
+
if (this.sshObj.debug) {
|
|
170
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo failed password prompt: Password: [" + this.sshObj.server.password + "]");
|
|
171
|
+
}
|
|
172
|
+
this.sshObj.pwSent = false;
|
|
173
|
+
return this._resetFromSudoError();
|
|
174
|
+
case standardPrompt:
|
|
175
|
+
if (this.sshObj.debug) {
|
|
176
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo password accepted");
|
|
177
|
+
}
|
|
178
|
+
this.sshObj.pwSent = false;
|
|
179
|
+
if (this.sshObj.verbose) {
|
|
180
|
+
this.emit('msg', this.sshObj.server.host + ": Sudo Standard Response: " + response);
|
|
181
|
+
}
|
|
182
|
+
return this._commandComplete();
|
|
183
|
+
default:
|
|
184
|
+
return this.idleTimer = setTimeout((function(_this) {
|
|
185
|
+
return function() {
|
|
186
|
+
return _this.emit('commandTimeout', _this.command, response, _this._stream, _this._connection);
|
|
187
|
+
};
|
|
188
|
+
})(this), this.idleTime);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
SSH2Shell.prototype._resetFromSudoError = function() {
|
|
193
|
+
this.sshObj.pwSent = false;
|
|
194
|
+
this.sshObj.sessionText += "" + this._buffer;
|
|
195
|
+
this._buffer = "";
|
|
196
|
+
this.command = "";
|
|
197
|
+
return this._stream.write('\x03');
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
SSH2Shell.prototype._processSSHPrompt = function() {
|
|
201
|
+
var passphrasePrompt, passwordPrompt, response, standardPrompt, using;
|
|
202
|
+
response = this._sanitizeResponse().trim();
|
|
203
|
+
if (this.idleTimer) {
|
|
204
|
+
clearTimeout(this.idleTimer);
|
|
205
|
+
}
|
|
206
|
+
passwordPrompt = this.passwordPrompt.test(response) && !this.sshObj.server.hasOwnProperty("passPhrase");
|
|
207
|
+
passphrasePrompt = this.passphrasePrompt.test(response) && this.sshObj.server.hasOwnProperty("passPhrase");
|
|
208
|
+
standardPrompt = this.standardPrompt.test(response);
|
|
209
|
+
if (this.sshObj.verbose) {
|
|
210
|
+
this.emit('msg', this.sshObj.server.host + ": SSH: Password previously sent: " + this.sshObj.sshAuth);
|
|
211
|
+
}
|
|
212
|
+
if (!this.sshObj.sshAuth) {
|
|
213
|
+
if (this.sshObj.debug) {
|
|
214
|
+
this.emit('msg', this.sshObj.server.host + ": First SSH prompt detection");
|
|
215
|
+
}
|
|
216
|
+
if (passwordPrompt) {
|
|
217
|
+
if (this.sshObj.debug) {
|
|
218
|
+
this.emit('msg', this.sshObj.server.host + ": SSH send password");
|
|
219
|
+
}
|
|
220
|
+
this.sshObj.sshAuth = true;
|
|
221
|
+
this._buffer = "";
|
|
222
|
+
return this._runCommand("" + this.sshObj.server.password);
|
|
223
|
+
} else if (passphrasePrompt) {
|
|
224
|
+
if (this.sshObj.debug) {
|
|
225
|
+
this.emit('msg', this.sshObj.server.host + ": SSH send passphrase");
|
|
226
|
+
}
|
|
227
|
+
this.sshObj.sshAuth = true;
|
|
228
|
+
this._buffer = "";
|
|
229
|
+
return this._runCommand("" + this.sshObj.server.passPhrase);
|
|
230
|
+
} else if (standardPrompt) {
|
|
231
|
+
if (this.sshObj.debug) {
|
|
232
|
+
this.emit('msg', this.sshObj.server.host + ": SSH standard prompt: connection failed");
|
|
233
|
+
}
|
|
234
|
+
this.emit('msg', this.sshObj.server.host + ": SSH connection failed");
|
|
235
|
+
this.emit('msg', this.sshObj.server.host + ": SSH failed response: " + response);
|
|
236
|
+
this.sshObj.sessionText += this.sshObj.server.host + ": SSH failed: response: " + response;
|
|
237
|
+
return this._runExit();
|
|
238
|
+
} else {
|
|
239
|
+
this.emit('msg', this.sshObj.server.host + ": SSH no prompt was not detected");
|
|
240
|
+
if (this.sshObj.verbose) {
|
|
241
|
+
this.emit('msg', this.sshObj.server.host + ": SSH response: " + response);
|
|
242
|
+
}
|
|
243
|
+
if (this.sshObj.verbose && !this.sshObj.server.hasOwnProperty("password")) {
|
|
244
|
+
this.emit('msg', this.sshObj.server.host + ": SSH password prompt: " + this.sshObj.passwordPrompt);
|
|
245
|
+
}
|
|
246
|
+
if (this.sshObj.verbose && this.sshObj.server.hasOwnProperty("passPhrase")) {
|
|
247
|
+
this.emit('msg', this.sshObj.server.host + ": SSH passphrase prompt: " + this.sshObj.passphrasePrompt);
|
|
248
|
+
}
|
|
249
|
+
if (this.sshObj.verbose) {
|
|
250
|
+
this.emit('msg', this.sshObj.server.host + ": SSH standard prompt: " + this.sshObj.standardPrompt);
|
|
251
|
+
}
|
|
252
|
+
return this._runExit();
|
|
253
|
+
}
|
|
254
|
+
} else {
|
|
255
|
+
if (this.sshObj.debug) {
|
|
256
|
+
this.emit('msg', this.sshObj.server.host + ": SSH post authentication prompt detection");
|
|
257
|
+
}
|
|
258
|
+
if (standardPrompt) {
|
|
259
|
+
if (this.sshObj.debug) {
|
|
260
|
+
this.emit('msg', this.sshObj.server.host + ": SSH complete: normal prompt");
|
|
261
|
+
}
|
|
262
|
+
if (this.sshObj.showBanner) {
|
|
263
|
+
this.sshObj.sessionText += "\n " + response;
|
|
264
|
+
}
|
|
265
|
+
this.sshObj.exitCommands.push("exit");
|
|
266
|
+
this.emit('msg', "" + this.sshObj.connectedMessage);
|
|
267
|
+
this.emit('msg', "" + this.sshObj.readyMessage);
|
|
268
|
+
if (this.sshObj.debug) {
|
|
269
|
+
this.emit('msg', this.sshObj.server.host + ": SSH complete: next command");
|
|
270
|
+
}
|
|
271
|
+
this.sshObj.sshToNextHost = false;
|
|
272
|
+
return this._nextCommand();
|
|
273
|
+
} else if (passwordPrompt || passphrasePrompt) {
|
|
274
|
+
this.emit('msg', this.sshObj.server.host + ": SSH authentication failed");
|
|
275
|
+
if (this.sshObj.debug) {
|
|
276
|
+
this.emit('msg', this.sshObj.server.host + ": SSH auth failed");
|
|
277
|
+
}
|
|
278
|
+
if (this.sshObj.verbose) {
|
|
279
|
+
this.emit('msg', this.sshObj.server.host + ": SSH: failed response: " + response);
|
|
280
|
+
}
|
|
281
|
+
this.sshObj.sshAuth = false;
|
|
282
|
+
using = (function() {
|
|
283
|
+
switch (false) {
|
|
284
|
+
case !passwordPrompt:
|
|
285
|
+
return "SSH password: " + this.sshObj.server.password;
|
|
286
|
+
case !passphrasePrompt:
|
|
287
|
+
return "SSH passphrase: " + this.sshObj.server.passPhrase;
|
|
288
|
+
}
|
|
289
|
+
}).call(this);
|
|
290
|
+
this.emit('error', this.sshObj.server.host + ": SSH authentication failed for " + this.sshObj.server.userName + "@" + this.sshObj.server.host, "Nested host authentication");
|
|
291
|
+
if (this.sshObj.debug) {
|
|
292
|
+
this.emit('msg', (this.sshObj.server.host + ": SSH auth failed: Using ") + using);
|
|
293
|
+
}
|
|
294
|
+
if (this.sshObj.verbose) {
|
|
295
|
+
this.emit('msg', this.sshObj.server.host + ": SSH response: " + response);
|
|
296
|
+
}
|
|
297
|
+
if (this._connections.length > 0) {
|
|
298
|
+
return this._previousHost();
|
|
299
|
+
}
|
|
300
|
+
return this._runExit();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
SSH2Shell.prototype._processNotifications = function() {
|
|
306
|
+
var msgNote, sessionNote;
|
|
307
|
+
if (this.command) {
|
|
308
|
+
if ((sessionNote = this.command.match(/^`(.*)`$/))) {
|
|
309
|
+
if (this.sshObj.debug) {
|
|
310
|
+
this.emit('msg', this.sshObj.server.host + ": Notifications: sessionText output");
|
|
311
|
+
}
|
|
312
|
+
if (this._connections.length > 0) {
|
|
313
|
+
this.sshObj.sessionText += this.sshObj.server.host + ": Note: " + sessionNote[1] + this.sshObj.enter;
|
|
314
|
+
} else {
|
|
315
|
+
this.sshObj.sessionText += "Note: " + sessionNote[1] + this.sshObj.enter;
|
|
316
|
+
}
|
|
317
|
+
if (this.sshObj.verbose) {
|
|
318
|
+
this.emit('msg', sessionNote[1]);
|
|
319
|
+
}
|
|
320
|
+
return this._nextCommand();
|
|
321
|
+
} else if ((msgNote = this.command.match(/^msg:(.*)$/))) {
|
|
322
|
+
if (this.sshObj.debug) {
|
|
323
|
+
this.emit('msg', this.sshObj.server.host + ": Notifications: msg to output");
|
|
324
|
+
}
|
|
325
|
+
this.emit('msg', this.sshObj.server.host + ": Note: " + msgNote[1]);
|
|
326
|
+
return this._nextCommand();
|
|
327
|
+
} else {
|
|
328
|
+
if (this.sshObj.debug) {
|
|
329
|
+
this.emit('msg', this.sshObj.server.host + ": Notifications: Normal Command to run");
|
|
330
|
+
}
|
|
331
|
+
return this._checkCommand();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
SSH2Shell.prototype._commandComplete = function() {
|
|
337
|
+
var response;
|
|
338
|
+
response = this._buffer.trim();
|
|
339
|
+
if (this.command !== "" && this.command !== "exit" && this.sshObj.sshToNextHost !== true) {
|
|
340
|
+
if (this.sshObj.verbose) {
|
|
341
|
+
this.emit('msg', this.sshObj.server.host + ": Command complete:\nCommand:\n " + this.command + "\nResponse: " + response);
|
|
342
|
+
}
|
|
343
|
+
this.sshObj.sessionText += response;
|
|
344
|
+
if (this.sshObj.debug) {
|
|
345
|
+
this.emit('msg', this.sshObj.server.host + ": Raising commandComplete event");
|
|
346
|
+
}
|
|
347
|
+
this.emit('commandComplete', this.command, this._buffer, this.sshObj);
|
|
348
|
+
}
|
|
349
|
+
return this._nextCommand();
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
SSH2Shell.prototype._nextCommand = function() {
|
|
353
|
+
this._buffer = "";
|
|
354
|
+
if (this.sshObj.commands.length > 0) {
|
|
355
|
+
if (this.sshObj.verbose) {
|
|
356
|
+
this.emit('msg', this.sshObj.server.host + ": Host.commands: " + this.sshObj.commands);
|
|
357
|
+
}
|
|
358
|
+
this.command = this.sshObj.commands.shift();
|
|
359
|
+
if (this.sshObj.verbose) {
|
|
360
|
+
this.emit('msg', this.sshObj.server.host + ": Next command from host.commands: " + this.command);
|
|
361
|
+
}
|
|
362
|
+
return this._processNotifications();
|
|
363
|
+
} else {
|
|
364
|
+
if (this.sshObj.debug) {
|
|
365
|
+
this.emit('msg', this.sshObj.server.host + ": No commands so exit");
|
|
366
|
+
}
|
|
367
|
+
return this._runExit();
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
SSH2Shell.prototype._checkCommand = function() {
|
|
372
|
+
if (this.command !== "") {
|
|
373
|
+
return this._runCommand(this.command);
|
|
374
|
+
} else {
|
|
375
|
+
if (this.sshObj.debug) {
|
|
376
|
+
this.emit('msg', this.sshObj.server.host + ": No command so exit");
|
|
377
|
+
}
|
|
378
|
+
return this._runExit();
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
SSH2Shell.prototype._runCommand = function(command) {
|
|
383
|
+
if (this.sshObj.verbose) {
|
|
384
|
+
this.emit('msg', this.sshObj.server.host + ": sending: " + command);
|
|
385
|
+
}
|
|
386
|
+
if (this.sshObj.debug) {
|
|
387
|
+
this.emit('msg', this.sshObj.server.host + ": run command");
|
|
388
|
+
}
|
|
389
|
+
return this._stream.write("" + command + this.sshObj.enter);
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
SSH2Shell.prototype._previousHost = function() {
|
|
393
|
+
if (this.sshObj.debug) {
|
|
394
|
+
this.emit('msg', this.sshObj.server.host + ": Load previous host config");
|
|
395
|
+
}
|
|
396
|
+
this.emit('end', this.sshObj.server.host + ": \n" + this.sshObj.sessionText, this.sshObj);
|
|
397
|
+
if (this.sshObj.debug) {
|
|
398
|
+
this.emit('msg', this.sshObj.server.host + ": Previous hosts: " + this._connections.length);
|
|
399
|
+
}
|
|
400
|
+
if (this._connections.length > 0) {
|
|
401
|
+
this.sshObj = this._connections.pop();
|
|
402
|
+
if (this.sshObj.debug) {
|
|
403
|
+
this.emit('msg', this.sshObj.server.host + ": Reload previous host");
|
|
404
|
+
}
|
|
405
|
+
return this._runExit();
|
|
406
|
+
} else {
|
|
407
|
+
return this._runExit();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
SSH2Shell.prototype._nextHost = function() {
|
|
412
|
+
var nextHost;
|
|
413
|
+
nextHost = this.sshObj.hosts.shift();
|
|
414
|
+
if (this.sshObj.debug) {
|
|
415
|
+
this.emit('msg', this.sshObj.server.host + ": SSH to " + nextHost.server.host);
|
|
416
|
+
}
|
|
417
|
+
if (this.sshObj.debug) {
|
|
418
|
+
this.emit('msg', this.sshObj.server.host + ": Clearing previous event handlers");
|
|
419
|
+
}
|
|
420
|
+
if (this.sshObj.debug) {
|
|
421
|
+
this.emit('msg', this.sshObj.server.host + ": Remove previous event handlers");
|
|
422
|
+
}
|
|
423
|
+
this._removeListeners();
|
|
424
|
+
this._connections.push(this.sshObj);
|
|
425
|
+
this.sshObj = nextHost;
|
|
426
|
+
return this._initiate(this._sshConnect);
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
SSH2Shell.prototype._nextPrimaryHost = function(connect) {
|
|
430
|
+
if (this._hosts.length > 0) {
|
|
431
|
+
if (this.sshObj.server) {
|
|
432
|
+
if (this.sshObj.debug) {
|
|
433
|
+
this.emit('msg', this.sshObj.server.host + ": Current primary host");
|
|
434
|
+
}
|
|
435
|
+
if (this.sshObj.debug) {
|
|
436
|
+
this.emit('msg', this.sshObj.server.host + ": Remove previous event handlers");
|
|
437
|
+
}
|
|
438
|
+
this.emit('end', this._primaryhostSessionText, this.sshObj);
|
|
439
|
+
this._removeListeners();
|
|
440
|
+
}
|
|
441
|
+
this.sshObj = this._hosts.shift();
|
|
442
|
+
this._primaryhostSessionText = this.sshObj.server.host + ": ";
|
|
443
|
+
if (this.sshObj.debug) {
|
|
444
|
+
this.emit('msg', this.sshObj.server.host + ": Next primary host");
|
|
445
|
+
}
|
|
446
|
+
return this._initiate(connect);
|
|
447
|
+
} else {
|
|
448
|
+
if (this.sshObj.debug) {
|
|
449
|
+
this.emit('msg', this.sshObj.server.host + ": No more primary hosts");
|
|
450
|
+
}
|
|
451
|
+
return this._runExit;
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
SSH2Shell.prototype._sshConnect = function() {
|
|
456
|
+
var option, ref, shellQuote, sshFlags, sshOptions, value;
|
|
457
|
+
sshFlags = "-x";
|
|
458
|
+
sshOptions = "";
|
|
459
|
+
if (this.sshObj.server.ssh) {
|
|
460
|
+
if (this.sshObj.server.ssh.forceProtocolVersion) {
|
|
461
|
+
sshFlags += this.sshObj.server.ssh.forceProtocolVersion;
|
|
462
|
+
}
|
|
463
|
+
if (this.sshObj.server.ssh.forceAddressType) {
|
|
464
|
+
sshFlags += this.sshObj.server.ssh.forceAddressType;
|
|
465
|
+
}
|
|
466
|
+
if (this.sshObj.server.ssh.disablePseudoTTY) {
|
|
467
|
+
sshFlags += "T";
|
|
468
|
+
}
|
|
469
|
+
if (this.sshObj.server.ssh.forcePseudoTTY) {
|
|
470
|
+
sshFlags += "t";
|
|
471
|
+
}
|
|
472
|
+
if (this.sshObj.server.ssh.verbose) {
|
|
473
|
+
sshFlags += "v";
|
|
474
|
+
}
|
|
475
|
+
if (this.sshObj.server.ssh.cipherSpec) {
|
|
476
|
+
sshOptions += " -c " + this.sshObj.server.ssh.cipherSpec;
|
|
477
|
+
}
|
|
478
|
+
if (this.sshObj.server.ssh.escape) {
|
|
479
|
+
sshOptions += " -e " + this.sshObj.server.ssh.escape;
|
|
480
|
+
}
|
|
481
|
+
if (this.sshObj.server.ssh.logFile) {
|
|
482
|
+
sshOptions += " -E " + this.sshObj.server.ssh.logFile;
|
|
483
|
+
}
|
|
484
|
+
if (this.sshObj.server.ssh.configFile) {
|
|
485
|
+
sshOptions += " -F " + this.sshObj.server.ssh.configFile;
|
|
486
|
+
}
|
|
487
|
+
if (this.sshObj.server.ssh.identityFile) {
|
|
488
|
+
sshOptions += " -i " + this.sshObj.server.ssh.identityFile;
|
|
489
|
+
}
|
|
490
|
+
if (this.sshObj.server.ssh.loginName) {
|
|
491
|
+
sshOptions += " -l " + this.sshObj.server.ssh.loginName;
|
|
492
|
+
}
|
|
493
|
+
if (this.sshObj.server.ssh.macSpec) {
|
|
494
|
+
sshOptions += " -m " + this.sshObj.server.ssh.macSpec;
|
|
495
|
+
}
|
|
496
|
+
shellQuote = function(value) {
|
|
497
|
+
return "'" + String(value).replace(/'/g, "'\\''") + "'";
|
|
498
|
+
};
|
|
499
|
+
ref = this.sshObj.server.ssh.options || {};
|
|
500
|
+
for (option in ref) {
|
|
501
|
+
value = ref[option];
|
|
502
|
+
sshOptions += " -o " + shellQuote(option + "=" + value);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
sshOptions += ' -o "StrictHostKeyChecking=no"';
|
|
506
|
+
sshOptions += " -p " + this.sshObj.server.port;
|
|
507
|
+
this.sshObj.sshAuth = false;
|
|
508
|
+
this.sshObj.sshToNextHost = true;
|
|
509
|
+
this.command = "ssh " + sshFlags + " " + sshOptions + " " + this.sshObj.server.userName + "@" + this.sshObj.server.host;
|
|
510
|
+
if (this.sshObj.debug) {
|
|
511
|
+
this.emit('msg', this.sshObj.server.host + ": SSH command: connect");
|
|
512
|
+
}
|
|
513
|
+
return this._runCommand(this.command);
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
SSH2Shell.prototype._runExit = function() {
|
|
517
|
+
if (this.sshObj.debug) {
|
|
518
|
+
this.emit('msg', this.sshObj.server.host + ": Process an exit");
|
|
519
|
+
}
|
|
520
|
+
if (this.sshObj.exitCommands && this.sshObj.exitCommands.length > 0) {
|
|
521
|
+
if (this.sshObj.debug) {
|
|
522
|
+
this.emit('msg', this.sshObj.server.host + ": Queued exit commands: " + this.sshObj.exitCommands.length);
|
|
523
|
+
}
|
|
524
|
+
this.command = this.sshObj.exitCommands.pop();
|
|
525
|
+
if (this._connections && this._connections.length > 0) {
|
|
526
|
+
if (this.sshObj.verbose) {
|
|
527
|
+
this.emit('msg', this.sshObj.server.host + ": Primary host: " + this._connections[0].server.host);
|
|
528
|
+
}
|
|
529
|
+
this._connections[0].sessionText += "\n" + this.sshObj.server.host + ": " + this.sshObj.sessionText;
|
|
530
|
+
}
|
|
531
|
+
return this._runCommand(this.command);
|
|
532
|
+
} else if (this.sshObj.hosts && this.sshObj.hosts.length > 0) {
|
|
533
|
+
if (this.sshObj.debug) {
|
|
534
|
+
this.emit('msg', this.sshObj.server.host + ": Next host from this host");
|
|
535
|
+
}
|
|
536
|
+
return this._nextHost();
|
|
537
|
+
} else if (this._connections && this._connections.length > 0) {
|
|
538
|
+
if (this.sshObj.debug) {
|
|
539
|
+
this.emit('msg', this.sshObj.server.host + ": load previous host");
|
|
540
|
+
}
|
|
541
|
+
this.emit('msg', this.sshObj.server.host + ": " + this.sshObj.closedMessage);
|
|
542
|
+
return this._previousHost();
|
|
543
|
+
} else if (this.command === "exit") {
|
|
544
|
+
if (this.sshObj.debug) {
|
|
545
|
+
this.emit('msg', this.sshObj.server.host + ": Manual exit command");
|
|
546
|
+
}
|
|
547
|
+
return this._runCommand("exit");
|
|
548
|
+
} else {
|
|
549
|
+
return this.close();
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
SSH2Shell.prototype.close = function() {
|
|
554
|
+
if (this.sshObj.debug) {
|
|
555
|
+
this.emit('msg', this.sshObj.server.host + ": Exit command: Stream: close");
|
|
556
|
+
}
|
|
557
|
+
return this._stream.close();
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
SSH2Shell.prototype._removeListeners = function() {
|
|
561
|
+
|
|
562
|
+
/*
|
|
563
|
+
if @sshObj.debug
|
|
564
|
+
@.emit 'msg', "#{@sshObj.server.host}: Event handler count:"
|
|
565
|
+
@.emit 'msg', "keyboard-interactive: " + (@.listenerCount 'keyboard-interactive')
|
|
566
|
+
@.emit 'msg', "error: " + (@.listenerCount "error")
|
|
567
|
+
@.emit 'msg', "data: " + (@.listenerCount "data")
|
|
568
|
+
@.emit 'msg', "stderrData: " + (@.listenerCount "stderrData")
|
|
569
|
+
@.emit 'msg', "end: " + (@.listenerCount 'end')
|
|
570
|
+
@.emit 'msg', "commandProcessing: " + (@.listenerCount 'commandProcessing')
|
|
571
|
+
@.emit 'msg', "commandComplete: " + (@.listenerCount 'commandComplete')
|
|
572
|
+
@.emit 'msg', "commandTimeout: " + (@.listenerCount 'commandTimeout')
|
|
573
|
+
@.emit 'msg', "msg: " + (@.listenerCount 'msg')
|
|
574
|
+
*/
|
|
575
|
+
if (typeof this.sshObj.onKeyboardInteractive === 'function') {
|
|
576
|
+
this.removeListener("keyboard-interactive", this.sshObj.onKeyboardInteractive);
|
|
577
|
+
}
|
|
578
|
+
if (typeof this.sshObj.onStderrData === 'function') {
|
|
579
|
+
this.removeListener("stderrData", this.sshObj.onStderrData);
|
|
580
|
+
}
|
|
581
|
+
if (typeof this.sshObj.onData === 'function') {
|
|
582
|
+
this.removeListener("data", this.sshObj.onData);
|
|
583
|
+
}
|
|
584
|
+
if (typeof this.sshObj.onError === 'function') {
|
|
585
|
+
this.removeListener("error", this.sshObj.onError);
|
|
586
|
+
}
|
|
587
|
+
if (typeof this.sshObj.onStderrData === 'function') {
|
|
588
|
+
this.removeListener("stderrData", this.sshObj.onStderrData);
|
|
589
|
+
}
|
|
590
|
+
if (typeof this.sshObj.onEnd === 'function') {
|
|
591
|
+
this.removeListener("end", this.sshObj.onEnd);
|
|
592
|
+
}
|
|
593
|
+
if (typeof this.sshObj.onCommandProcessing === 'function') {
|
|
594
|
+
this.removeListener("commandProcessing", this.sshObj.onCommandProcessing);
|
|
595
|
+
}
|
|
596
|
+
if (typeof this.sshObj.onCommandComplete === 'function') {
|
|
597
|
+
this.removeListener("commandComplete", this.sshObj.onCommandComplete);
|
|
598
|
+
}
|
|
599
|
+
if (typeof this.sshObj.onCommandTimeout === 'function') {
|
|
600
|
+
this.removeListener("commandTimeout", this.sshObj.onCommandTimeout);
|
|
601
|
+
}
|
|
602
|
+
if (typeof this.sshObj.msg === 'function') {
|
|
603
|
+
this.removeListener("msg", this.sshObj.msg);
|
|
604
|
+
}
|
|
605
|
+
if (this.idleTimer) {
|
|
606
|
+
clearTimeout(this.idleTimer);
|
|
607
|
+
}
|
|
608
|
+
if (this.dataReceivedTimer) {
|
|
609
|
+
return clearTimeout(this.dataReceivedTimer);
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
function SSH2Shell(hosts) {
|
|
614
|
+
this._connect = bind(this._connect, this);
|
|
615
|
+
this.connect = bind(this.connect, this);
|
|
616
|
+
this._loadDefaults = bind(this._loadDefaults, this);
|
|
617
|
+
this._initiate = bind(this._initiate, this);
|
|
618
|
+
this._removeListeners = bind(this._removeListeners, this);
|
|
619
|
+
this.close = bind(this.close, this);
|
|
620
|
+
this._runExit = bind(this._runExit, this);
|
|
621
|
+
this._sshConnect = bind(this._sshConnect, this);
|
|
622
|
+
this._nextPrimaryHost = bind(this._nextPrimaryHost, this);
|
|
623
|
+
this._nextHost = bind(this._nextHost, this);
|
|
624
|
+
this._previousHost = bind(this._previousHost, this);
|
|
625
|
+
this._runCommand = bind(this._runCommand, this);
|
|
626
|
+
this._checkCommand = bind(this._checkCommand, this);
|
|
627
|
+
this._nextCommand = bind(this._nextCommand, this);
|
|
628
|
+
this._commandComplete = bind(this._commandComplete, this);
|
|
629
|
+
this._processNotifications = bind(this._processNotifications, this);
|
|
630
|
+
this._processSSHPrompt = bind(this._processSSHPrompt, this);
|
|
631
|
+
this._resetFromSudoError = bind(this._resetFromSudoError, this);
|
|
632
|
+
this._processPasswordPrompt = bind(this._processPasswordPrompt, this);
|
|
633
|
+
this._sanitizeResponse = bind(this._sanitizeResponse, this);
|
|
634
|
+
this._onData = bind(this._onData, this);
|
|
635
|
+
this.onEnd = bind(this.onEnd, this);
|
|
636
|
+
this.onCommandTimeout = bind(this.onCommandTimeout, this);
|
|
637
|
+
this.onCommandComplete = bind(this.onCommandComplete, this);
|
|
638
|
+
this.onCommandProcessing = bind(this.onCommandProcessing, this);
|
|
639
|
+
this._callback = bind(this._callback, this);
|
|
640
|
+
SSH2Shell.__super__.constructor.call(this, hosts);
|
|
641
|
+
if (typeIsArray(hosts) && hosts.length > 0) {
|
|
642
|
+
this._hosts = hosts;
|
|
643
|
+
} else {
|
|
644
|
+
this._hosts = [hosts];
|
|
645
|
+
}
|
|
646
|
+
this.ssh2Client = require('ssh2').Client;
|
|
647
|
+
this.on("msg", (function(_this) {
|
|
648
|
+
return function(message) {
|
|
649
|
+
return console.log(message);
|
|
650
|
+
};
|
|
651
|
+
})(this));
|
|
652
|
+
this.on("newPrimmaryHost", this._nextPrimaryHost);
|
|
653
|
+
this.on("data", (function(_this) {
|
|
654
|
+
return function(data) {
|
|
655
|
+
return _this._onData(data);
|
|
656
|
+
};
|
|
657
|
+
})(this));
|
|
658
|
+
this.on("stderrData", (function(_this) {
|
|
659
|
+
return function(data) {
|
|
660
|
+
return console.error(data);
|
|
661
|
+
};
|
|
662
|
+
})(this));
|
|
663
|
+
this.on("error", (function(_this) {
|
|
664
|
+
return function(err, type, close, callback) {
|
|
665
|
+
if (close == null) {
|
|
666
|
+
close = false;
|
|
667
|
+
}
|
|
668
|
+
if (_this.sshObj.debug) {
|
|
669
|
+
_this.emit('msg', "Class.error: " + err + ", " + type);
|
|
670
|
+
}
|
|
671
|
+
if (err instanceof Error) {
|
|
672
|
+
_this.emit('msg', "Error: " + err.message + ", Level: " + err.level);
|
|
673
|
+
} else {
|
|
674
|
+
_this.emit('msg', (type + " error: ") + err);
|
|
675
|
+
}
|
|
676
|
+
if (close) {
|
|
677
|
+
return _this._stream.close();
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
})(this));
|
|
681
|
+
if (this._hosts[0].server.tryKeyboard) {
|
|
682
|
+
this.on("keyboard-interactive", (function(_this) {
|
|
683
|
+
return function(name, instructions, instructionsLang, prompts, finish) {
|
|
684
|
+
var str;
|
|
685
|
+
if (_this.sshObj.debug) {
|
|
686
|
+
_this.emit('msg', _this.sshObj.server.host + ": Class.keyboard-interactive");
|
|
687
|
+
}
|
|
688
|
+
if (_this.sshObj.verbose) {
|
|
689
|
+
_this.emit('msg', "name: " + name);
|
|
690
|
+
_this.emit('msg', "instructions: " + instructions);
|
|
691
|
+
str = JSON.stringify(prompts, null, 4);
|
|
692
|
+
return _this.emit('msg', "Prompts object: " + str);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
})(this));
|
|
696
|
+
}
|
|
697
|
+
this._allSessions = "";
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
SSH2Shell.prototype._initiate = function(action) {
|
|
701
|
+
if (this.sshObj.debug) {
|
|
702
|
+
this.emit('msg', this.sshObj.server.host + ": initiate");
|
|
703
|
+
}
|
|
704
|
+
this._loadDefaults();
|
|
705
|
+
if (typeof action === 'function') {
|
|
706
|
+
return action();
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
SSH2Shell.prototype._loadDefaults = function() {
|
|
711
|
+
var ref, ref1, ref2, ref3, ref4, ref5, ref6;
|
|
712
|
+
if (this.sshObj.msg && this.sshObj.msg.send && typeof this.sshObj.msg.send === 'function') {
|
|
713
|
+
this.sshObj.msg = this.sshObj.msg.send;
|
|
714
|
+
}
|
|
715
|
+
if (this.listenerCount("msg") > 0) {
|
|
716
|
+
this.removeAllListeners("msg");
|
|
717
|
+
}
|
|
718
|
+
if (typeof this.sshObj.msg === 'function') {
|
|
719
|
+
this.on("msg", this.sshObj.msg);
|
|
720
|
+
} else {
|
|
721
|
+
this.on("msg", (function(_this) {
|
|
722
|
+
return function(message) {
|
|
723
|
+
return console.log(message);
|
|
724
|
+
};
|
|
725
|
+
})(this));
|
|
726
|
+
}
|
|
727
|
+
if (this.sshObj.debug) {
|
|
728
|
+
this.emit('msg', this.sshObj.server.host + ": Load Defaults");
|
|
729
|
+
}
|
|
730
|
+
this.command = "";
|
|
731
|
+
this._buffer = "";
|
|
732
|
+
this.sshObj.sshToNextHost = false;
|
|
733
|
+
if (!this.sshObj.connectedMessage) {
|
|
734
|
+
this.sshObj.connectedMessage = "Connected";
|
|
735
|
+
}
|
|
736
|
+
if (!this.sshObj.readyMessage) {
|
|
737
|
+
this.sshObj.readyMessage = "Ready";
|
|
738
|
+
}
|
|
739
|
+
if (!this.sshObj.closedMessage) {
|
|
740
|
+
this.sshObj.closedMessage = "Closed";
|
|
741
|
+
}
|
|
742
|
+
if (!this.sshObj.showBanner) {
|
|
743
|
+
this.sshObj.showBanner = false;
|
|
744
|
+
}
|
|
745
|
+
if (!this.sshObj.verbose) {
|
|
746
|
+
this.sshObj.verbose = false;
|
|
747
|
+
}
|
|
748
|
+
if (!this.sshObj.debug) {
|
|
749
|
+
this.sshObj.debug = false;
|
|
750
|
+
}
|
|
751
|
+
if (!this.sshObj.hosts) {
|
|
752
|
+
this.sshObj.hosts = [];
|
|
753
|
+
}
|
|
754
|
+
if (!this.sshObj.commands) {
|
|
755
|
+
this.sshObj.commands = [];
|
|
756
|
+
}
|
|
757
|
+
if (!this.sshObj.standardPrompt) {
|
|
758
|
+
this.sshObj.standardPrompt = ">$%#";
|
|
759
|
+
}
|
|
760
|
+
if (!this.sshObj.passwordPrompt) {
|
|
761
|
+
this.sshObj.passwordPrompt = this.sshObj.passwordPromt;
|
|
762
|
+
}
|
|
763
|
+
if (!this.sshObj.passphrasePrompt) {
|
|
764
|
+
this.sshObj.passphrasePrompt = this.sshObj.passphrasePromt;
|
|
765
|
+
}
|
|
766
|
+
if (!this.sshObj.passwordPrompt) {
|
|
767
|
+
this.sshObj.passwordPrompt = ":";
|
|
768
|
+
}
|
|
769
|
+
if (!this.sshObj.passphrasePrompt) {
|
|
770
|
+
this.sshObj.passphrasePrompt = ":";
|
|
771
|
+
}
|
|
772
|
+
if (!this.sshObj.passPromptText) {
|
|
773
|
+
this.sshObj.passPromptText = "Password";
|
|
774
|
+
}
|
|
775
|
+
if (!this.sshObj.enter) {
|
|
776
|
+
this.sshObj.enter = "\n";
|
|
777
|
+
}
|
|
778
|
+
if (!this.sshObj.asciiFilter) {
|
|
779
|
+
this.sshObj.asciiFilter = "[^\r\n\x20-\x7e]";
|
|
780
|
+
}
|
|
781
|
+
if (this.sshObj.disableColorFilter !== true) {
|
|
782
|
+
this.sshObj.disableColorFilter = false;
|
|
783
|
+
}
|
|
784
|
+
if (this.sshObj.disableASCIIFilter !== true) {
|
|
785
|
+
this.sshObj.disableASCIIFilter = false;
|
|
786
|
+
}
|
|
787
|
+
if (!this.sshObj.textColorFilter) {
|
|
788
|
+
this.sshObj.textColorFilter = "(\[{1}[0-9;]+m{1})";
|
|
789
|
+
}
|
|
790
|
+
if (!this.sshObj.exitCommands) {
|
|
791
|
+
this.sshObj.exitCommands = [];
|
|
792
|
+
}
|
|
793
|
+
this.sshObj.pwSent = false;
|
|
794
|
+
this.sshObj.sshAuth = false;
|
|
795
|
+
this.sshObj.server.hashKey = (ref = this.sshObj.server.hashKey) != null ? ref : "";
|
|
796
|
+
if (!this.sshObj.sessionText) {
|
|
797
|
+
this.sshObj.sessionText = "";
|
|
798
|
+
}
|
|
799
|
+
this.sshObj.streamEncoding = (ref1 = this.sshObj.streamEncoding) != null ? ref1 : "utf8";
|
|
800
|
+
if (!this.sshObj.window) {
|
|
801
|
+
this.sshObj.window = true;
|
|
802
|
+
}
|
|
803
|
+
if (!this.sshObj.pty) {
|
|
804
|
+
this.sshObj.pty = true;
|
|
805
|
+
}
|
|
806
|
+
this.idleTime = (ref2 = this.sshObj.idleTimeOut) != null ? ref2 : 5000;
|
|
807
|
+
this.dataIdleTime = (ref3 = this.sshObj.dataIdleTime) != null ? ref3 : 500;
|
|
808
|
+
if (!this.asciiFilter) {
|
|
809
|
+
this.asciiFilter = new RegExp(this.sshObj.asciiFilter, "g");
|
|
810
|
+
}
|
|
811
|
+
if (!this.textColorFilter) {
|
|
812
|
+
this.textColorFilter = new RegExp(this.sshObj.textColorFilter, "g");
|
|
813
|
+
}
|
|
814
|
+
if (!this.passwordPrompt) {
|
|
815
|
+
this.passwordPrompt = new RegExp(this.sshObj.passPromptText + ".*" + this.sshObj.passwordPrompt + "\\s?$", "i");
|
|
816
|
+
}
|
|
817
|
+
if (!this.passphrasePrompt) {
|
|
818
|
+
this.passphrasePrompt = new RegExp(this.sshObj.passPromptText + ".*" + this.sshObj.passphrasePrompt + "\\s?$", "i");
|
|
819
|
+
}
|
|
820
|
+
if (!this.standardPrompt) {
|
|
821
|
+
this.standardPrompt = new RegExp("[" + this.sshObj.standardPrompt + "]\\s?$");
|
|
822
|
+
}
|
|
823
|
+
this.sshObj.onCommandProcessing = (ref4 = this.sshObj.onCommandProcessing) != null ? ref4 : (function(_this) {
|
|
824
|
+
return function(command, response, sshObj, stream) {};
|
|
825
|
+
})(this);
|
|
826
|
+
this.sshObj.onCommandComplete = (ref5 = this.sshObj.onCommandComplete) != null ? ref5 : (function(_this) {
|
|
827
|
+
return function(command, response, sshObj) {
|
|
828
|
+
if (_this.sshObj.debug) {
|
|
829
|
+
return _this.emit('msg', _this.sshObj.server.host + ": Class.commandComplete");
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
})(this);
|
|
833
|
+
this.sshObj.onCommandTimeout = (ref6 = this.sshObj.onCommandTimeout) != null ? ref6 : (function(_this) {
|
|
834
|
+
return function(command, response, stream, connection) {
|
|
835
|
+
response = response.replace(_this.command, "");
|
|
836
|
+
if (_this.sshObj.debug) {
|
|
837
|
+
_this.emit('msg', _this.sshObj.server.host + ": Class.commandTimeout");
|
|
838
|
+
}
|
|
839
|
+
if (_this.sshObj.verbose) {
|
|
840
|
+
_this.emit('msg', _this.sshObj.server.host + ": Timeout command: " + command + " response: " + response);
|
|
841
|
+
}
|
|
842
|
+
_this.emit("error", _this.sshObj.server.host + ": Command timed out after " + (_this.idleTime / 1000) + " seconds", "Timeout", true, function(err, type) {});
|
|
843
|
+
return _this.sshObj.sessionText += _this._buffer;
|
|
844
|
+
};
|
|
845
|
+
})(this);
|
|
846
|
+
if (typeof this.sshObj.onKeyboardInteractive === 'function') {
|
|
847
|
+
this.on("keyboard-interactive", this.sshObj.onKeyboardInteractive);
|
|
848
|
+
}
|
|
849
|
+
if (typeof this.sshObj.onError === 'function') {
|
|
850
|
+
this.on("error", this.sshObj.onError);
|
|
851
|
+
}
|
|
852
|
+
if (typeof this.sshObj.onData === 'function') {
|
|
853
|
+
this.on("data", this.sshObj.onData);
|
|
854
|
+
}
|
|
855
|
+
if (typeof this.sshObj.onStderrData === 'function') {
|
|
856
|
+
this.on("stderrData", this.sshObj.onStderrData);
|
|
857
|
+
}
|
|
858
|
+
this.on("commandProcessing", this.sshObj.onCommandProcessing);
|
|
859
|
+
this.on("commandComplete", this.sshObj.onCommandComplete);
|
|
860
|
+
this.on("commandTimeout", this.sshObj.onCommandTimeout);
|
|
861
|
+
if (typeof this.sshObj.onEnd === 'function') {
|
|
862
|
+
this.on("end", this.sshObj.onEnd);
|
|
863
|
+
}
|
|
864
|
+
if (this.sshObj.debug) {
|
|
865
|
+
this.emit('msg', this.sshObj.server.host + ": Host loaded");
|
|
866
|
+
}
|
|
867
|
+
if (this.sshObj.verbose) {
|
|
868
|
+
return this.emit('msg', this.sshObj);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
SSH2Shell.prototype.connect = function(callback) {
|
|
873
|
+
if (typeof callback === 'function') {
|
|
874
|
+
this._callback = callback;
|
|
875
|
+
}
|
|
876
|
+
return this.emit("newPrimaryHost", this._nextPrimaryHost(this._connect));
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
SSH2Shell.prototype._connect = function() {
|
|
880
|
+
var e;
|
|
881
|
+
this.connection = new this.ssh2Client();
|
|
882
|
+
this.connection.on("keyboard-interactive", (function(_this) {
|
|
883
|
+
return function(name, instructions, instructionsLang, prompts, finish) {
|
|
884
|
+
if (_this.sshObj.debug) {
|
|
885
|
+
_this.emit('msg', _this.sshObj.server.host + ": Connection.keyboard-interactive");
|
|
886
|
+
}
|
|
887
|
+
return _this.emit("keyboard-interactive", name, instructions, instructionsLang, prompts, finish);
|
|
888
|
+
};
|
|
889
|
+
})(this));
|
|
890
|
+
this.connection.on("connect", (function(_this) {
|
|
891
|
+
return function() {
|
|
892
|
+
if (_this.sshObj.debug) {
|
|
893
|
+
_this.emit('msg', _this.sshObj.server.host + ": Connection.connect");
|
|
894
|
+
}
|
|
895
|
+
return _this.emit('msg', _this.sshObj.connectedMessage);
|
|
896
|
+
};
|
|
897
|
+
})(this));
|
|
898
|
+
this.connection.on("ready", (function(_this) {
|
|
899
|
+
return function() {
|
|
900
|
+
if (_this.sshObj.debug) {
|
|
901
|
+
_this.emit('msg', _this.sshObj.server.host + ": Connection.ready");
|
|
902
|
+
}
|
|
903
|
+
_this.emit('msg', _this.sshObj.readyMessage);
|
|
904
|
+
return _this.connection.shell(_this.sshObj.window, {
|
|
905
|
+
pty: _this.sshObj.pty
|
|
906
|
+
}, function(err, _stream) {
|
|
907
|
+
_this._stream = _stream;
|
|
908
|
+
if (err instanceof Error) {
|
|
909
|
+
_this.emit('error', err, "Shell", true);
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
if (_this.sshObj.debug) {
|
|
913
|
+
_this.emit('msg', _this.sshObj.server.host + ": Connection.shell");
|
|
914
|
+
}
|
|
915
|
+
_this._stream.setEncoding(_this.sshObj.streamEncoding);
|
|
916
|
+
_this._stream.on("error", function(err) {
|
|
917
|
+
if (_this.sshObj.debug) {
|
|
918
|
+
_this.emit('msg', _this.sshObj.server.host + ": Stream.error");
|
|
919
|
+
}
|
|
920
|
+
return _this.emit('error', err, "Stream");
|
|
921
|
+
});
|
|
922
|
+
_this._stream.stderr.on('data', function(data) {
|
|
923
|
+
if (_this.sshObj.debug) {
|
|
924
|
+
_this.emit('msg', _this.sshObj.server.host + ": Stream.stderr.data");
|
|
925
|
+
}
|
|
926
|
+
return _this.emit('stderrData', data);
|
|
927
|
+
});
|
|
928
|
+
_this._stream.on("data", function(data) {
|
|
929
|
+
var e;
|
|
930
|
+
try {
|
|
931
|
+
return _this.emit('data', data);
|
|
932
|
+
} catch (error) {
|
|
933
|
+
e = error;
|
|
934
|
+
err = new Error(e + " " + e.stack);
|
|
935
|
+
err.level = "Data handling";
|
|
936
|
+
return _this.emit('error', err, "Stream.read", true);
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
_this._stream.on("finish", function() {
|
|
940
|
+
if (_this.sshObj.debug) {
|
|
941
|
+
_this.emit('msg', _this.sshObj.server.host + ": Stream.finish");
|
|
942
|
+
}
|
|
943
|
+
_this._primaryhostSessionText += _this.sshObj.sessionText + "\n";
|
|
944
|
+
_this._allSessions += _this._primaryhostSessionText;
|
|
945
|
+
if (_this._hosts.length === 0) {
|
|
946
|
+
return _this.emit('end', _this._allSessions, _this.sshObj);
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
return _this._stream.on("close", function(code, signal) {
|
|
950
|
+
if (_this.sshObj.debug) {
|
|
951
|
+
_this.emit('msg', _this.sshObj.server.host + ": Stream.close");
|
|
952
|
+
}
|
|
953
|
+
return _this.connection.end();
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
};
|
|
957
|
+
})(this));
|
|
958
|
+
this.connection.on("error", (function(_this) {
|
|
959
|
+
return function(err) {
|
|
960
|
+
if (_this.sshObj.debug) {
|
|
961
|
+
_this.emit('msg', _this.sshObj.server.host + ": Connection.error");
|
|
962
|
+
}
|
|
963
|
+
return _this.emit("error", err, "Connection");
|
|
964
|
+
};
|
|
965
|
+
})(this));
|
|
966
|
+
this.connection.on("close", (function(_this) {
|
|
967
|
+
return function(had_error) {
|
|
968
|
+
if (_this.sshObj.debug) {
|
|
969
|
+
_this.emit('msg', _this.sshObj.server.host + ": Connection.close");
|
|
970
|
+
}
|
|
971
|
+
if (_this.idleTimer) {
|
|
972
|
+
clearTimeout(_this.idleTimer);
|
|
973
|
+
}
|
|
974
|
+
if (_this.dataReceivedTimer) {
|
|
975
|
+
clearTimeout(_this.dataReceivedTimer);
|
|
976
|
+
}
|
|
977
|
+
if (had_error) {
|
|
978
|
+
_this.emit("error", had_error, "Connection close");
|
|
979
|
+
} else {
|
|
980
|
+
_this.emit('msg', _this.sshObj.closedMessage);
|
|
981
|
+
}
|
|
982
|
+
if (_this._hosts.length === 0) {
|
|
983
|
+
if (typeof _this._callback === 'function') {
|
|
984
|
+
_this._callback(_this._allSessions);
|
|
985
|
+
_this._removeListeners();
|
|
986
|
+
return _this._allSessions;
|
|
987
|
+
}
|
|
988
|
+
} else {
|
|
989
|
+
return _this.emit("newPrimaryHost", _this._nextPrimaryHost(_this._connect));
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
})(this));
|
|
993
|
+
if (this.sshObj.server && this.sshObj.commands) {
|
|
994
|
+
try {
|
|
995
|
+
if (!this.sshObj.server.username) {
|
|
996
|
+
this.sshObj.server.username = this.sshObj.server.userName;
|
|
997
|
+
}
|
|
998
|
+
if (!this.sshObj.server.hostHash) {
|
|
999
|
+
this.sshObj.server.hostHash = this.sshObj.server.hashMethod;
|
|
1000
|
+
}
|
|
1001
|
+
if (!this.sshObj.server.passphrase) {
|
|
1002
|
+
this.sshObj.server.passphrase = this.sshObj.server.passPhrase;
|
|
1003
|
+
}
|
|
1004
|
+
this.connection.connect(this.sshObj.server);
|
|
1005
|
+
} catch (error) {
|
|
1006
|
+
e = error;
|
|
1007
|
+
this.emit('error', e + " " + e.stack, "Connection.connect", true);
|
|
1008
|
+
}
|
|
1009
|
+
} else {
|
|
1010
|
+
this.emit('error', "Missing connection parameters", "Parameters", false, function(err, type, close) {
|
|
1011
|
+
this.emit('msg', this.sshObj.server);
|
|
1012
|
+
return this.emit('msg', this.sshObj.commands);
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
return this._stream;
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
return SSH2Shell;
|
|
1019
|
+
|
|
1020
|
+
})(Stream);
|
|
1021
|
+
|
|
1022
|
+
module.exports = SSH2Shell;
|
|
1023
|
+
|
|
1024
|
+
}).call(this);
|