-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
47 lines (42 loc) · 1.24 KB
/
handler.js
File metadata and controls
47 lines (42 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const ssh2 = require('ssh2');
const Client = ssh2.Client;
const fs = require('fs');
exports.sshExec = (params) => {
return new Promise(function(resolve, reject) {
// Set Private Key
let privateKey = params.PRIVATE_KEY || null;
if (privateKey && fs.lstatSync(privateKey).isFile()) {
privateKey = fs.readFileSync(privateKey) ;
}
const cmd = params.CMD;
const connectionOptions = {
host: params.HOST,
port: params.PORT || '22',
username: params.USER_NAME,
passphrase: params.PASSPHRASE || null,
password: params.PASSWORD || null,
privateKey,
};
const conn = new Client();
conn.on('ready', function() {
conn.exec(cmd, {'pty': true}, function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
reject({
close: 'Stream :: close :: code: ' + code + ', signal: ' + signal
});
conn.end();
}).on('data', function(data) {
conn.end();
resolve({
stdout: data.toString()
})
}).stderr.on('data', function(data) {
reject({
stderr: data.toString()
});
});
});
}).connect(connectionOptions);
});
}