@ohos-ports/simple-ssh 0.0.0-stage → 1.1.1-beta.1
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/.jshintrc +25 -0
- package/Gruntfile.js +49 -0
- package/LICENSE.txt +22 -0
- package/README.md +264 -2
- package/config/ssh.json +6 -0
- package/lib/ssh.d.ts +149 -0
- package/lib/ssh.js +317 -0
- package/package.json +35 -5
- package/test/ssh.js +267 -0
package/.jshintrc
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"curly": true,
|
|
3
|
+
"eqeqeq": true,
|
|
4
|
+
"immed": true,
|
|
5
|
+
"newcap": true,
|
|
6
|
+
"noarg": true,
|
|
7
|
+
"noempty": true,
|
|
8
|
+
"nonew": true,
|
|
9
|
+
"quotmark": "single",
|
|
10
|
+
"undef": true,
|
|
11
|
+
"unused": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
|
|
14
|
+
"globalstrict": true,
|
|
15
|
+
|
|
16
|
+
"devel": true,
|
|
17
|
+
"node": true,
|
|
18
|
+
|
|
19
|
+
"globals": {
|
|
20
|
+
"afterEach": true,
|
|
21
|
+
"beforeEach": true,
|
|
22
|
+
"describe": true,
|
|
23
|
+
"it": true
|
|
24
|
+
}
|
|
25
|
+
}
|
package/Gruntfile.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function(grunt) {
|
|
4
|
+
|
|
5
|
+
var PROJECT_FILES = ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js'];
|
|
6
|
+
|
|
7
|
+
grunt.loadNpmTasks('grunt-contrib-jshint');
|
|
8
|
+
grunt.loadNpmTasks('grunt-contrib-watch');
|
|
9
|
+
grunt.loadNpmTasks('grunt-simple-mocha');
|
|
10
|
+
|
|
11
|
+
grunt.initConfig({
|
|
12
|
+
jshint: {
|
|
13
|
+
options: {
|
|
14
|
+
jshintrc: '.jshintrc'
|
|
15
|
+
},
|
|
16
|
+
all: {
|
|
17
|
+
files: {
|
|
18
|
+
src: PROJECT_FILES
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
watch: {
|
|
24
|
+
all: {
|
|
25
|
+
files: PROJECT_FILES,
|
|
26
|
+
tasks: ['jshint:all']
|
|
27
|
+
},
|
|
28
|
+
test: {
|
|
29
|
+
files: PROJECT_FILES,
|
|
30
|
+
tasks: ['jshint:all', 'simplemocha:all']
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
simplemocha: {
|
|
35
|
+
options: {
|
|
36
|
+
timeout: 10000,
|
|
37
|
+
ui: 'bdd',
|
|
38
|
+
reporter: 'spec'
|
|
39
|
+
},
|
|
40
|
+
all: {
|
|
41
|
+
src: ['test/**/*.js']
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
grunt.registerTask('default', ['jshint:all', 'simplemocha:all']);
|
|
47
|
+
grunt.registerTask('test', 'watch:test');
|
|
48
|
+
|
|
49
|
+
};
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Mike Cluck
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
|
4
|
+
obtaining a copy of this software and associated documentation
|
|
5
|
+
files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use,
|
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the
|
|
9
|
+
Software is furnished to do so, subject to the following
|
|
10
|
+
conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,265 @@
|
|
|
1
|
-
|
|
1
|
+
**Note:** *I really don't work on this project anymore. It's still fully functional, I'm just not interested in working on it anymore. If you post an issue, I might respond and I might not. If you submit a pull request, I'll most likely accept it. Hopefully it continues to help people around the world.* :earth_americas:
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# simple-ssh
|
|
4
|
+
|
|
5
|
+
A wrapper for the [ssh2 client module by Brian White][1] which makes it easier to run a sequence of commands over SSH.
|
|
6
|
+
|
|
7
|
+
# Requirements
|
|
8
|
+
|
|
9
|
+
* [node.js][2] -- v0.8.7 or newer
|
|
10
|
+
|
|
11
|
+
# Install
|
|
12
|
+
|
|
13
|
+
npm install simple-ssh
|
|
14
|
+
|
|
15
|
+
# Examples
|
|
16
|
+
|
|
17
|
+
* Echoing out a users `PATH`:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
var SSH = require('simple-ssh');
|
|
21
|
+
|
|
22
|
+
var ssh = new SSH({
|
|
23
|
+
host: 'localhost',
|
|
24
|
+
user: 'username',
|
|
25
|
+
pass: 'password'
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
ssh.exec('echo $PATH', {
|
|
29
|
+
out: function(stdout) {
|
|
30
|
+
console.log(stdout);
|
|
31
|
+
}
|
|
32
|
+
}).start();
|
|
33
|
+
|
|
34
|
+
/*** Using the `args` options instead ***/
|
|
35
|
+
ssh.exec('echo', {
|
|
36
|
+
args: ['$PATH'],
|
|
37
|
+
out: function(stdout) {
|
|
38
|
+
console.log(stdout);
|
|
39
|
+
}
|
|
40
|
+
}).start();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
* Connecting with the active SSH Agent with Agent Forwarding
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
var ssh = new ssh({
|
|
47
|
+
host: 'localhost',
|
|
48
|
+
user: 'username',
|
|
49
|
+
agent: process.env.SSH_AUTH_SOCK,
|
|
50
|
+
agentForward: true
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
* Capturing error output:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
ssh.exec('this-does-not-exist', {
|
|
58
|
+
err: function(stderr) {
|
|
59
|
+
console.log(stderr); // this-does-not-exist: command not found
|
|
60
|
+
}
|
|
61
|
+
}).start();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
* Capturing error codes:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
ssh.exec('exit 69', {
|
|
68
|
+
exit: function(code) {
|
|
69
|
+
console.log(code); // 69
|
|
70
|
+
}
|
|
71
|
+
}).start();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
* Sending data to stdin:
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
ssh.exec('cat > /path/to/remote/file', {
|
|
78
|
+
in: fs.readFileSync('/path/to/local/file')
|
|
79
|
+
}).start();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
* Chaining commands together:
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
ssh
|
|
86
|
+
.exec('echo "Node.js"', {
|
|
87
|
+
out: console.log.bind(console)
|
|
88
|
+
})
|
|
89
|
+
.exec('echo "is"', {
|
|
90
|
+
out: console.log.bind(console)
|
|
91
|
+
})
|
|
92
|
+
.exec('echo "awesome!"', {
|
|
93
|
+
out: console.log.bind(console)
|
|
94
|
+
})
|
|
95
|
+
.start();
|
|
96
|
+
|
|
97
|
+
// Output:
|
|
98
|
+
// Node.js
|
|
99
|
+
// is
|
|
100
|
+
// awesome!
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
* Get the number of commands:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
ssh
|
|
107
|
+
.exec('exit 1')
|
|
108
|
+
.exec('exit 2')
|
|
109
|
+
.exec('exit 3');
|
|
110
|
+
|
|
111
|
+
console.log(ssh.count()); // 3
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
* Running a command using `sudo`
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
ssh.exec('sudo echo "Pseudo-sudo"', {
|
|
118
|
+
pty: true,
|
|
119
|
+
out: console.log.bind(console)
|
|
120
|
+
}).start();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
* Resetting a connection and the commands
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
// Echos out any messages the user sent in if 10 or more have been queued
|
|
127
|
+
var msgInterval = setInterval(function() {
|
|
128
|
+
if (ssh.count() > 10) {
|
|
129
|
+
ssh.start();
|
|
130
|
+
}
|
|
131
|
+
}, 1000);
|
|
132
|
+
|
|
133
|
+
socket.on('message', function(msg) {
|
|
134
|
+
// If a 'reset' message is received, clear previous messages
|
|
135
|
+
if (msg === 'reset') {
|
|
136
|
+
ssh.reset(function(err) {
|
|
137
|
+
if (err) {
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
ssh.exec('echo "reset"');
|
|
142
|
+
});
|
|
143
|
+
} else {
|
|
144
|
+
ssh.exec('echo "' + msg + '"');
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
* Listening for additional events
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
ssh.on('error', function(err) {
|
|
153
|
+
console.log('Oops, something went wrong.');
|
|
154
|
+
console.log(err);
|
|
155
|
+
ssh.end();
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
* Event handlers can be chained as well
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
ssh
|
|
163
|
+
.on('error', onSSHError)
|
|
164
|
+
.on('ready', onSSHReady);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
----------
|
|
168
|
+
|
|
169
|
+
# API
|
|
170
|
+
|
|
171
|
+
### Functions
|
|
172
|
+
|
|
173
|
+
* **Constructor**( [ _config_ ] )
|
|
174
|
+
* **config** { _Object_ }:
|
|
175
|
+
* **config.host** { _String_ }: Hostname
|
|
176
|
+
* **config.port** { _Number_ }: Port number (default: `22`)
|
|
177
|
+
* **config.user** { _String_ }: Username
|
|
178
|
+
* **config.pass** { _String_ }: Password
|
|
179
|
+
* **config.timeout** { _Number_ }: Connection timeout in milliseconds (default: `10000`)
|
|
180
|
+
* **config.key** { _String_ }: SSH key
|
|
181
|
+
* **config.passphrase** { _String_ }: Passphrase
|
|
182
|
+
* **config.baseDir** { _String_ }: Base directory. If this is set, each command will be preceeded by `cd ${this.baseDir}`
|
|
183
|
+
* **config.agent** { _String_ }: Connects with the given SSH agent. If this is set, no need to specify a private key or password.
|
|
184
|
+
* **config.agentForward** { _Boolean_ }: Set to true to connect with agent forwarding.
|
|
185
|
+
* **exec**( _command_, [ _options_ ] ): **Adds a command to the stack**
|
|
186
|
+
* **command** { _String_ }: Command to be executed
|
|
187
|
+
* **options** { _Object_ }:
|
|
188
|
+
* **options.args** { _String[]_ }: Additional command line arguments (default: `null`)
|
|
189
|
+
* **options.in** { _String_ }: Input to be sent to `stdin`
|
|
190
|
+
* **options.out** { _Function( stdout )_ }: `stdout` handler
|
|
191
|
+
* **stdout** { _String_ }: Output streamed through `stdout`
|
|
192
|
+
* **options.err** { _Function( stderr )_ }: `stderr` handler
|
|
193
|
+
* **stderr** { _String_ }: Output streamed through `stderr`
|
|
194
|
+
* **options.exit** { _Function( code, stdout, stderr )_ }: Exit handler
|
|
195
|
+
* **code** { _Number_ }: Exit code
|
|
196
|
+
* **stdout** { _String_ }: All of the standard output concatenated together
|
|
197
|
+
* **stderr** { _String_ }: All of the error output concatenated together
|
|
198
|
+
* **options.pty** { _Boolean_ }: Allocates a pseudo-tty, useful for command which require `sudo` (default: `false`)
|
|
199
|
+
* **on**( _event_, _callback_ ): **Add a listener for the specified event** (Courtesy of [@alexjab][3])
|
|
200
|
+
* **event** { _String_ }: Event to listen to
|
|
201
|
+
* **callback** { _Function_ }: Executed on the event
|
|
202
|
+
* **start**( [ _options_ ] ): **Starts executing the commands**
|
|
203
|
+
* **options** { _Object_ }:
|
|
204
|
+
* **options.success** { _Function()_ }: Called on successful connection
|
|
205
|
+
* **options.fail** { _Function( err )_ }: Called if the connection failed
|
|
206
|
+
* **err** { _Error_ }: Error information
|
|
207
|
+
* **reset**( [ _callback_ ] ): **Clears the command queue and resets the current connection**
|
|
208
|
+
* **callback** { _Function( err )_ }: Called when the connection has been successfully reset
|
|
209
|
+
* **err** { _Error_ }: Error information
|
|
210
|
+
* **end**(): **Ends the SSH session** (this is automatically called at the end of a command queue).
|
|
211
|
+
|
|
212
|
+
### Properties
|
|
213
|
+
|
|
214
|
+
* **host** { _String_ }: Host to connect to
|
|
215
|
+
* **port** { _Number_ }: Port to connect through (default: `22`)
|
|
216
|
+
* **user** { _String_ }: User name
|
|
217
|
+
* **pass** { _String_ }: Password
|
|
218
|
+
* **timeout** { _Number_ }: Connection timeout in milliseconds (default: `10000`)
|
|
219
|
+
* **key** { _String_ }: SSH key
|
|
220
|
+
* **baseDir** { _String_ }: If set, will change directory to `baseDir` before each command
|
|
221
|
+
|
|
222
|
+
### Flow Control
|
|
223
|
+
|
|
224
|
+
Sometimes you may find yourself needing to change which commands are executed. The flow can be changed by returning `false` from an `exit` handler.
|
|
225
|
+
|
|
226
|
+
**Note**: This only works if `false` is explicitly returned. "Falsy" values are not sufficient (since `undefined` is implicitly returned and it's "falsy").
|
|
227
|
+
|
|
228
|
+
* Ending prematurely:
|
|
229
|
+
|
|
230
|
+
```javascript
|
|
231
|
+
ssh
|
|
232
|
+
.exec('pwd', {
|
|
233
|
+
exit: function() {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
.exec('echo "Not executed"')
|
|
238
|
+
.start();
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
* Running a new queue of commands:
|
|
242
|
+
|
|
243
|
+
```javascript
|
|
244
|
+
ssh
|
|
245
|
+
.exec('exit', {
|
|
246
|
+
args: [ Math.round(Math.random()) ],
|
|
247
|
+
exit: function(code) {
|
|
248
|
+
if (code === 1) {
|
|
249
|
+
// Setup the new command queue
|
|
250
|
+
ssh.exec('echo "new queue"');
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
})
|
|
255
|
+
.exec('exit 0', {
|
|
256
|
+
exit: function() {
|
|
257
|
+
console.log('Previous command did not return false');
|
|
258
|
+
}
|
|
259
|
+
})
|
|
260
|
+
.start();
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
[1]: https://github.com/mscdex/ssh2
|
|
264
|
+
[2]: http://nodejs.org
|
|
265
|
+
[3]: https://github.com/alexjab
|
package/config/ssh.json
ADDED
package/lib/ssh.d.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
interface WithPassword {
|
|
2
|
+
/**
|
|
3
|
+
* Password
|
|
4
|
+
*/
|
|
5
|
+
pass: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface WithKey {
|
|
9
|
+
/**
|
|
10
|
+
* SSH key
|
|
11
|
+
*/
|
|
12
|
+
key: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Password
|
|
16
|
+
*/
|
|
17
|
+
pass?: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Passphrase
|
|
21
|
+
*/
|
|
22
|
+
passphrase?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface WithAgent {
|
|
26
|
+
/**
|
|
27
|
+
* Connects with the given SSH agent.
|
|
28
|
+
* If this is set, no need to specify a private key or password.
|
|
29
|
+
*/
|
|
30
|
+
agent: string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Set to true to connect with agent forwarding.
|
|
34
|
+
*/
|
|
35
|
+
agentForward: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Authentication = WithAgent | WithKey | WithPassword;
|
|
39
|
+
|
|
40
|
+
interface BaseConfig {
|
|
41
|
+
/**
|
|
42
|
+
* Hostname
|
|
43
|
+
*/
|
|
44
|
+
host: string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Port number (default: `22`)
|
|
48
|
+
*/
|
|
49
|
+
port?: number;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Username
|
|
53
|
+
*/
|
|
54
|
+
user: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Connection timeout in milliseconds.
|
|
58
|
+
* Defaults to `10000`
|
|
59
|
+
*/
|
|
60
|
+
timeout?: number;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Base directory.
|
|
64
|
+
* If this is set, each command will be preceeded by `cd ${this.baseDir}`
|
|
65
|
+
*/
|
|
66
|
+
baseDir?: string;
|
|
67
|
+
}
|
|
68
|
+
export type SSHConfig = BaseConfig & Authentication;
|
|
69
|
+
|
|
70
|
+
export interface ExecOptions {
|
|
71
|
+
/**
|
|
72
|
+
* Additional command line arguments (default: `null`)
|
|
73
|
+
*/
|
|
74
|
+
args?: string[] | null;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Input to be sent to `stdin`
|
|
78
|
+
*/
|
|
79
|
+
in?: string;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* `stdout` handler
|
|
83
|
+
*/
|
|
84
|
+
out?: (stdout: string) => unknown;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* `stderr` handler
|
|
88
|
+
*/
|
|
89
|
+
err?: (stderr: string) => unknown;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Exit handler.
|
|
93
|
+
* `stderr` and `stdout` contain the full contents of their respective streams
|
|
94
|
+
*/
|
|
95
|
+
exit?: (code: number, stdout: string, stderr: string) => unknown;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Allocates a pseudo-tty, useful for command which require `sudo` (default: `false`)
|
|
99
|
+
*/
|
|
100
|
+
pty?: boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface StartOptions {
|
|
104
|
+
/**
|
|
105
|
+
* Called on successful connection
|
|
106
|
+
*/
|
|
107
|
+
success?: () => unknown;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Called if the connection failed
|
|
111
|
+
*/
|
|
112
|
+
fail?: (err: Error) => unknown;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export default class SSH {
|
|
116
|
+
constructor(config: SSHConfig);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Adds a command to the stack
|
|
120
|
+
*/
|
|
121
|
+
exec(command: string, options?: ExecOptions): SSH;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Add a listener for the specified event
|
|
125
|
+
*/
|
|
126
|
+
on(event: string, callback: (...args: any[]) => unknown): SSH;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Starts executing the commands
|
|
130
|
+
*/
|
|
131
|
+
start(options?: StartOptions): SSH;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Clears the command queue and resets the current connection
|
|
135
|
+
* @param callback Called when the connection has been successfully reset
|
|
136
|
+
*/
|
|
137
|
+
reset(callback?: (err?: Error) => unknown): SSH;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Ends the SSH session
|
|
141
|
+
* (this is automatically called at the end of a command queue)
|
|
142
|
+
*/
|
|
143
|
+
end(): SSH;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Returns the number of commands
|
|
147
|
+
*/
|
|
148
|
+
count(): number;
|
|
149
|
+
}
|
package/lib/ssh.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Client = require('@ohos-ports/ssh2').Client,
|
|
4
|
+
extend = require('xtend');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new SSH connection
|
|
8
|
+
* @param {Object} config
|
|
9
|
+
* @param {String} config.host - Host to connect to
|
|
10
|
+
* @param {Number} [config.port=22] - Port number
|
|
11
|
+
* @param {String} config.user - Username
|
|
12
|
+
* @param {String} config.pass - Password
|
|
13
|
+
* @param {String} [config.passphrase=''] - Passphrase
|
|
14
|
+
* @param {String} [config.baseDir=''] - Base directory to perform commands from
|
|
15
|
+
* @param {String} [config.agent=''] - SSH Agent to connect with
|
|
16
|
+
* @param {String} [config.agentForward=''] - Use agent forwarding
|
|
17
|
+
* @constructor
|
|
18
|
+
*/
|
|
19
|
+
var SSH = function(config) {
|
|
20
|
+
config = extend({
|
|
21
|
+
host: '',
|
|
22
|
+
port: 22,
|
|
23
|
+
user: '',
|
|
24
|
+
pass: '',
|
|
25
|
+
passphrase: '',
|
|
26
|
+
timeout: 10000,
|
|
27
|
+
key: '',
|
|
28
|
+
baseDir: '',
|
|
29
|
+
agent: '',
|
|
30
|
+
agentForward: ''
|
|
31
|
+
}, config);
|
|
32
|
+
|
|
33
|
+
this._c = new Client();
|
|
34
|
+
this._commands = [];
|
|
35
|
+
|
|
36
|
+
this.host = config.host;
|
|
37
|
+
this.port = config.port;
|
|
38
|
+
this.user = config.user;
|
|
39
|
+
this.pass = config.pass;
|
|
40
|
+
this.timeout = config.timeout;
|
|
41
|
+
this.key = config.key;
|
|
42
|
+
this.passphrase = config.passphrase;
|
|
43
|
+
this.baseDir = config.baseDir;
|
|
44
|
+
this.agent = config.agent;
|
|
45
|
+
this.agentForward = config.agentForward;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Helper function for queueing up the next command in the chain
|
|
50
|
+
* @param {Number} index - Array index of the command to perform next
|
|
51
|
+
* @private
|
|
52
|
+
*/
|
|
53
|
+
SSH.prototype._queueCommand = function(index) {
|
|
54
|
+
if (this._commands.length <= index) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
var self = this,
|
|
59
|
+
command = this._commands[index],
|
|
60
|
+
cmd = command.cmd;
|
|
61
|
+
|
|
62
|
+
// Start by going to the base directory, if set
|
|
63
|
+
if (this.baseDir.length > 0) {
|
|
64
|
+
cmd = 'cd ' + this.baseDir + ' && ' + cmd;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Skip command if not connected at the time of executing it
|
|
68
|
+
if (!(this._c._sock &&
|
|
69
|
+
this._c._sock.writable &&
|
|
70
|
+
this._c._sock._readableState &&
|
|
71
|
+
this._c._sock._readableState.ended === false)) {
|
|
72
|
+
command.handlers.err(new Error('Not connected'));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Execute the command
|
|
77
|
+
this._c.exec(cmd, { pty: command.pty }, function(err, stream) {
|
|
78
|
+
command.handlers.start();
|
|
79
|
+
if (err) {
|
|
80
|
+
command.handlers.err(err);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var passwordSent = false,
|
|
85
|
+
buffer = '',
|
|
86
|
+
stdout = '',
|
|
87
|
+
stderr = '';
|
|
88
|
+
|
|
89
|
+
stream.stderr.on('data', function(data) {
|
|
90
|
+
data = data.toString();
|
|
91
|
+
stderr += data;
|
|
92
|
+
command.handlers.err = command.handlers.err || function() {
|
|
93
|
+
console.log(data);
|
|
94
|
+
};
|
|
95
|
+
command.handlers.err(data);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
stream.on('data', function(data) {
|
|
99
|
+
// This will allow us to send in the password if we're doing something like `sudo`
|
|
100
|
+
buffer += (!passwordSent) ? data.toString() : '';
|
|
101
|
+
|
|
102
|
+
if (!passwordSent && buffer.substr(-2) === ': ') {
|
|
103
|
+
stream.write(self.pass + '\n');
|
|
104
|
+
buffer = '';
|
|
105
|
+
passwordSent = true;
|
|
106
|
+
} else {
|
|
107
|
+
data = data.toString();
|
|
108
|
+
stdout += data;
|
|
109
|
+
command.handlers.out(data);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
stream.on('close', function(code) {
|
|
114
|
+
var numOfCommands = self._commands.length,
|
|
115
|
+
executeNext = (command.handlers.exit(+code, stdout, stderr) !== false);
|
|
116
|
+
|
|
117
|
+
// Queue up the next command
|
|
118
|
+
if (executeNext && index < self._commands.length - 1) {
|
|
119
|
+
self._queueCommand(index + 1);
|
|
120
|
+
} else if (!executeNext) {
|
|
121
|
+
// If the commands have been modified during the last exit, use those
|
|
122
|
+
if (numOfCommands !== self._commands.length) {
|
|
123
|
+
self._commands.splice(0, numOfCommands);
|
|
124
|
+
self._queueCommand(0);
|
|
125
|
+
} else {
|
|
126
|
+
self._c.end();
|
|
127
|
+
}
|
|
128
|
+
} else if (index === self._commands.length - 1) {
|
|
129
|
+
self._c.end();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
if (command.in) {
|
|
133
|
+
stream.end(command.in);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Queues up a command for execution
|
|
140
|
+
* @param {String} command - Command to execute
|
|
141
|
+
* @param {Object} options
|
|
142
|
+
* @param {String[]} [options.args] - Additional command line arguments
|
|
143
|
+
* @param {Function(String)} [options.out] - Called to capture stdout
|
|
144
|
+
* @param {Function(String)} [options.err] - Called to capture stderr
|
|
145
|
+
* @param {Function(Number)} [options.exit] - Called when the command exits
|
|
146
|
+
* @param {Boolean} [options.pty=false] - If true, allocates a pseudo-tty (useful for sudo)
|
|
147
|
+
* @return {SSH}
|
|
148
|
+
*/
|
|
149
|
+
SSH.prototype.exec = function(command, options) {
|
|
150
|
+
options = extend({
|
|
151
|
+
args: null,
|
|
152
|
+
start: function() {},
|
|
153
|
+
in: null,
|
|
154
|
+
out: function() {},
|
|
155
|
+
err: function() {},
|
|
156
|
+
exit: function() {},
|
|
157
|
+
pty: false
|
|
158
|
+
}, options);
|
|
159
|
+
|
|
160
|
+
// Apply additional command line arguments
|
|
161
|
+
if (options.args !== null) {
|
|
162
|
+
command += ' ' + options.args.join(' ');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this._commands.push({
|
|
166
|
+
cmd: command,
|
|
167
|
+
in: options.in,
|
|
168
|
+
handlers: {
|
|
169
|
+
start: options.start,
|
|
170
|
+
out: options.out,
|
|
171
|
+
err: options.err,
|
|
172
|
+
exit: options.exit
|
|
173
|
+
},
|
|
174
|
+
pty: options.pty
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return this;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Starts executing the queued up commands
|
|
182
|
+
* @param {Object} [options]
|
|
183
|
+
* @param {Function} [options.success] - Executed once connected
|
|
184
|
+
* @param {Function(Error)} [options.fail] - Executed if we fail to connect
|
|
185
|
+
*/
|
|
186
|
+
SSH.prototype.start = function(options) {
|
|
187
|
+
options = extend({
|
|
188
|
+
success: function() {},
|
|
189
|
+
fail: function() {}
|
|
190
|
+
}, options);
|
|
191
|
+
var self = this;
|
|
192
|
+
|
|
193
|
+
self._c.on('error', options.fail);
|
|
194
|
+
|
|
195
|
+
self._c.on('ready', function() {
|
|
196
|
+
self._c.removeListener('error', options.fail);
|
|
197
|
+
options.success();
|
|
198
|
+
|
|
199
|
+
if (self._commands.length > 0) {
|
|
200
|
+
self._queueCommand(0);
|
|
201
|
+
} else {
|
|
202
|
+
self._c.end();
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
if (self.pass && !self.key) {
|
|
206
|
+
self._c.connect({
|
|
207
|
+
host: self.host,
|
|
208
|
+
port: self.port || 22,
|
|
209
|
+
username: self.user,
|
|
210
|
+
password: self.pass,
|
|
211
|
+
readyTimeout: self.timeout
|
|
212
|
+
});
|
|
213
|
+
} else if (!self.pass && self.key) {
|
|
214
|
+
if (self.passphrase) {
|
|
215
|
+
self._c.connect({
|
|
216
|
+
host: self.host,
|
|
217
|
+
port: self.port || 22,
|
|
218
|
+
username: self.user,
|
|
219
|
+
privateKey: self.key,
|
|
220
|
+
passphrase: self.passphrase,
|
|
221
|
+
readyTimeout: self.timeout
|
|
222
|
+
});
|
|
223
|
+
} else {
|
|
224
|
+
self._c.connect({
|
|
225
|
+
host: self.host,
|
|
226
|
+
port: self.port || 22,
|
|
227
|
+
username: self.user,
|
|
228
|
+
privateKey: self.key,
|
|
229
|
+
readyTimeout: self.timeout
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
} else if ((self.passphrase || self.pass) && self.key) {
|
|
233
|
+
try {
|
|
234
|
+
self._c.connect({
|
|
235
|
+
host: self.host,
|
|
236
|
+
port: self.port || 22,
|
|
237
|
+
username: self.user,
|
|
238
|
+
privateKey: self.key,
|
|
239
|
+
passphrase: self.passphrase || self.pass,
|
|
240
|
+
readyTimeout: self.timeout
|
|
241
|
+
});
|
|
242
|
+
} catch (err) {
|
|
243
|
+
self._c.emit('error', new Error('Incorrect passphrase: ' + err.toString()));
|
|
244
|
+
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
self._c.connect({
|
|
249
|
+
host: self.host,
|
|
250
|
+
port: self.port || 22,
|
|
251
|
+
username: self.user,
|
|
252
|
+
agent: self.agent,
|
|
253
|
+
agentForward: self.agentForward,
|
|
254
|
+
readyTimeout: self.timeout
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Resets the connection and commands
|
|
261
|
+
* @param {Function(Error)} [cb] - Executed when the connection and commands have been reset
|
|
262
|
+
*/
|
|
263
|
+
SSH.prototype.reset = function(cb) {
|
|
264
|
+
cb = cb || function() {};
|
|
265
|
+
if (this._c._sock !== undefined && this._c._state !== 'closed') {
|
|
266
|
+
var self = this;
|
|
267
|
+
this._c.on('close', function(err) {
|
|
268
|
+
self._c = new Client();
|
|
269
|
+
self._commands = [];
|
|
270
|
+
cb(err);
|
|
271
|
+
});
|
|
272
|
+
this._c.end();
|
|
273
|
+
} else {
|
|
274
|
+
this._c = new Client();
|
|
275
|
+
this._commands = [];
|
|
276
|
+
cb();
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Returns the number of commands in the queue
|
|
282
|
+
* @return {Number}
|
|
283
|
+
*/
|
|
284
|
+
SSH.prototype.count = function() {
|
|
285
|
+
return this._commands.length;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Ends the SSH connection
|
|
290
|
+
*/
|
|
291
|
+
SSH.prototype.end = function() {
|
|
292
|
+
this._c.end();
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* A wrapper method to access ssh2's events
|
|
297
|
+
* @param {String} event
|
|
298
|
+
* @param {Function} callback
|
|
299
|
+
* @return {SSH}
|
|
300
|
+
*/
|
|
301
|
+
SSH.prototype.on = function(event, callback) {
|
|
302
|
+
this._c.on(event, callback);
|
|
303
|
+
return this;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Removes an event handler from ssh2
|
|
308
|
+
* @param {String} event
|
|
309
|
+
* @param {Function} callback
|
|
310
|
+
* @return {SSH}
|
|
311
|
+
*/
|
|
312
|
+
SSH.prototype.off = function(event, callback) {
|
|
313
|
+
this._c.off(event, callback);
|
|
314
|
+
return this;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
module.exports = SSH;
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@ohos-ports/simple-ssh",
|
|
3
|
+
"version": "1.1.1-beta.1",
|
|
4
|
+
"description": "A wrapper for ssh2 to make it easier to perform commands over SSH",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ssh",
|
|
7
|
+
"ssh2"
|
|
8
|
+
],
|
|
9
|
+
"author": "Mike Cluck <mcluck90@gmail.com>",
|
|
10
|
+
"licenses": [
|
|
11
|
+
{
|
|
12
|
+
"type": "MIT",
|
|
13
|
+
"url": "http://github.com/MCluck90/simple-ssh/blob/master/LICENSE.txt"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib/ssh.js",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
20
|
+
"directory": "ports/simple-ssh/1.1.1"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"expect.js": "^0.3.1",
|
|
24
|
+
"grunt": "^1.4.1",
|
|
25
|
+
"grunt-contrib-jshint": "^3.1.1",
|
|
26
|
+
"grunt-contrib-watch": "^1.1.0",
|
|
27
|
+
"grunt-simple-mocha": "^0.4.1"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@ohos-ports/ssh2": "^1.17.0-beta.2",
|
|
31
|
+
"xtend": "^4.0.1"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/test/ssh.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var SSH = require('../lib/ssh.js'),
|
|
4
|
+
expect = require('expect.js'),
|
|
5
|
+
fs = require('fs'),
|
|
6
|
+
|
|
7
|
+
config = require('../config/ssh.json'),
|
|
8
|
+
ssh;
|
|
9
|
+
|
|
10
|
+
describe('SSH', function() {
|
|
11
|
+
if (config.key) {
|
|
12
|
+
config.key = fs.readFileSync(config.key);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
beforeEach(function() {
|
|
16
|
+
ssh = new SSH(config);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('start', function() {
|
|
20
|
+
it('should connect when given valid connection information', function(done) {
|
|
21
|
+
ssh.start({
|
|
22
|
+
success: done,
|
|
23
|
+
fail: function(err) {
|
|
24
|
+
expect().to.fail(err);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should fail to connect when given invalid information', function(done) {
|
|
30
|
+
ssh.host = 'This is not a valid hostname';
|
|
31
|
+
ssh.start({
|
|
32
|
+
success: function() {
|
|
33
|
+
expect().to.fail('Should not be able to connect with invalid information');
|
|
34
|
+
},
|
|
35
|
+
fail: function() {
|
|
36
|
+
done();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('end', function() {
|
|
43
|
+
it('should end the current connection', function(done) {
|
|
44
|
+
ssh._c.on('close', function() {
|
|
45
|
+
done();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
ssh.start({
|
|
49
|
+
success: function() {
|
|
50
|
+
ssh.end();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('exec', function() {
|
|
57
|
+
it('should queue up a command', function() {
|
|
58
|
+
ssh.exec('command');
|
|
59
|
+
expect(ssh._commands).to.have.length(1);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should append `args` as command line arguments', function() {
|
|
63
|
+
ssh.exec('cmd', {
|
|
64
|
+
args: ['a', 'b', 'c']
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
var command = ssh._commands[0].cmd;
|
|
68
|
+
expect(command).to.be('cmd a b c');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should capture stdout', function(done) {
|
|
72
|
+
var output = 'Johnny Grey';
|
|
73
|
+
ssh.exec('echo', {
|
|
74
|
+
args: ['"' + output + '"'],
|
|
75
|
+
out: function(stdout) {
|
|
76
|
+
expect(stdout.trim()).to.be(output);
|
|
77
|
+
done();
|
|
78
|
+
}
|
|
79
|
+
}).start();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should capture stderr', function(done) {
|
|
83
|
+
var output = 'Icarus';
|
|
84
|
+
ssh.exec('echo', {
|
|
85
|
+
args: ['"' + output + '"', '1>&2'],
|
|
86
|
+
err: function(stderr) {
|
|
87
|
+
expect(stderr.trim()).to.be(output);
|
|
88
|
+
done();
|
|
89
|
+
}
|
|
90
|
+
}).start();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should capture the exit code', function(done) {
|
|
94
|
+
var exitCode = Math.round(Math.random() * 200);
|
|
95
|
+
ssh.exec('exit', {
|
|
96
|
+
args: [exitCode],
|
|
97
|
+
exit: function(code) {
|
|
98
|
+
expect(code).to.be(exitCode);
|
|
99
|
+
done();
|
|
100
|
+
}
|
|
101
|
+
}).start();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should provide data on stdin', function(done) {
|
|
105
|
+
var input = 'The quick brown fox\njumps over the lazy dog.\n';
|
|
106
|
+
ssh.exec('cat', {
|
|
107
|
+
in: input,
|
|
108
|
+
out: function(stdout) {
|
|
109
|
+
expect(stdout).to.be(input);
|
|
110
|
+
done();
|
|
111
|
+
}
|
|
112
|
+
}).start();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should handle multiple commands', function(done) {
|
|
116
|
+
ssh
|
|
117
|
+
.exec('echo')
|
|
118
|
+
.exec('echo')
|
|
119
|
+
.exec('echo', {
|
|
120
|
+
exit: function() {
|
|
121
|
+
done();
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
.start();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should allow setting the base directory', function(done) {
|
|
128
|
+
ssh.baseDir = '/';
|
|
129
|
+
ssh
|
|
130
|
+
.exec('pwd', {
|
|
131
|
+
out: function(data) {
|
|
132
|
+
expect(data.trim()).to.be('/');
|
|
133
|
+
|
|
134
|
+
ssh.baseDir = '/home';
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
.exec('pwd', {
|
|
138
|
+
out: function(data) {
|
|
139
|
+
expect(data.trim()).to.be('/home');
|
|
140
|
+
done();
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
.start();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should handle `sudo` property when pty is set', function(done) {
|
|
147
|
+
var input = 'Excision',
|
|
148
|
+
output = '';
|
|
149
|
+
ssh.exec('sudo echo', {
|
|
150
|
+
pty: true,
|
|
151
|
+
args: ['"' + input + '"'],
|
|
152
|
+
out: function(data) {
|
|
153
|
+
data = data.trim();
|
|
154
|
+
output += data;
|
|
155
|
+
},
|
|
156
|
+
exit: function(code) {
|
|
157
|
+
expect(output).to.be(input);
|
|
158
|
+
expect(code).to.be(0);
|
|
159
|
+
done();
|
|
160
|
+
}
|
|
161
|
+
}).start();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should ignore upcoming commands when `exit` returns false', function(done) {
|
|
165
|
+
ssh
|
|
166
|
+
.exec('exit 0', {
|
|
167
|
+
exit: function() {
|
|
168
|
+
done();
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
.exec('exit 1', {
|
|
173
|
+
exit: function() {
|
|
174
|
+
expect().to.fail('Expected upcoming commands to be ignored');
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
.start();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('should use a new queue of commands when `exit` returns false', function(done) {
|
|
181
|
+
ssh
|
|
182
|
+
.exec('exit 0', {
|
|
183
|
+
exit: function() {
|
|
184
|
+
ssh.exec('exit 0', {
|
|
185
|
+
exit: function() {
|
|
186
|
+
done();
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
.exec('exit 1', {
|
|
194
|
+
exit: function() {
|
|
195
|
+
expect().to.fail('Command from previous queue should be removed');
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
.start();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('should append a new queue if `exit` does not return false', function(done) {
|
|
202
|
+
var callCount = 0;
|
|
203
|
+
ssh
|
|
204
|
+
.exec('exit 0', {
|
|
205
|
+
exit: function() {
|
|
206
|
+
expect(callCount).to.be(0);
|
|
207
|
+
callCount += 1;
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
.exec('exit 0', {
|
|
211
|
+
exit: function() {
|
|
212
|
+
expect(callCount).to.be(1);
|
|
213
|
+
callCount += 1;
|
|
214
|
+
|
|
215
|
+
ssh.exec('exit 0', {
|
|
216
|
+
exit: function() {
|
|
217
|
+
expect(callCount).to.be(3);
|
|
218
|
+
done();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
.exec('exit 0', {
|
|
224
|
+
exit: function() {
|
|
225
|
+
expect(callCount).to.be(2);
|
|
226
|
+
callCount += 1;
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
.start();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('reset', function() {
|
|
234
|
+
it('should create a new Connection object', function() {
|
|
235
|
+
var oldConnection = ssh._c;
|
|
236
|
+
ssh.reset();
|
|
237
|
+
expect(oldConnection).to.not.be(ssh._c);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should clear the commands queue', function() {
|
|
241
|
+
ssh
|
|
242
|
+
.exec('exit 0')
|
|
243
|
+
.exec('exit 1')
|
|
244
|
+
.exec('exit 2');
|
|
245
|
+
expect(ssh._commands).to.have.length(3);
|
|
246
|
+
|
|
247
|
+
ssh.reset();
|
|
248
|
+
expect(ssh._commands).to.have.length(0);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('should stop the current connection before replacing it', function(done) {
|
|
252
|
+
ssh
|
|
253
|
+
.exec('exit 0', {
|
|
254
|
+
exit: function() {
|
|
255
|
+
ssh.reset();
|
|
256
|
+
done();
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
.exec('exit 0', {
|
|
260
|
+
exit: function() {
|
|
261
|
+
expect().to.fail('Connection should have been ended');
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
.start();
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|