-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.js
31 lines (27 loc) · 829 Bytes
/
ssh.js
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
'use strict';
const { Client } = require('ssh2');
const fs = require('fs');
const config = {
host: process.env.REMOTE_HOST,
port: process.env.REMOTE_PORT,
username: process.env.REMOTE_USER,
privateKey: fs.readFileSync(process.env.REMOTE_KEYFILE)
};
const exec = (command, callback) => {
const conn = new Client();
let output;
conn.on('ready', () => {
conn.exec(command, (err, stream) => {
if (err) throw err;
stream.on('close', (code, signal) => {
conn.end();
callback(output);
}).on('data', (data) => {
if(data != null) output += data;
}).stderr.on('data', (data) => {
if(data != null) output += data;
});
});
}).connect(config);
}
exports.exec = exec;