-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.js
73 lines (62 loc) · 1.72 KB
/
shell.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const spawn = require('child_process').spawn;
const os = require('os');
let keygen = function (filePath, passphrase) {
return new Promise(function (resolve, reject) {
passphrase = passphrase || 'empty';
let options = ['-f', filePath];
let ls = spawn('ssh-keygen', options, {
stdio: 'inherit',
shell: true
});
ls.on('close', (code) => {
if (code === 0) {
resolve(code);
} else if (code === 512) {
reject(new Error('cancel create'))
} else {
reject(new Error('create error, error code:' + code))
}
});
ls.on('error', (error) => {
reject(error)
});
});
};
let copy = function (host, filePath, port) {
return new Promise(function (resolve, reject) {
let options = [];
let cmd = 'ssh-copy-id'
var platform = os.platform();
if (platform === 'win32') {
cmd = 'sh'
options.push('ssh-copy-id');
}
if (filePath && filePath !== '') {
options.push('-i');
options.push(filePath);
}
if (port) {
options.push('-p');
options.push(port + '');
}
options.push(host);
let ls = spawn(cmd, options, {
stdio: 'inherit',
shell: true
});
ls.on('close', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(new Error('copy fail, error code:' + code))
}
});
ls.on('error', (error) => {
reject(error)
});
});
};
module.exports = {
keygen,
copy
};