-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (52 loc) · 1.5 KB
/
index.js
File metadata and controls
61 lines (52 loc) · 1.5 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
48
49
50
51
52
53
54
55
56
57
58
const {Client} = require('ssh2');
const core = require('@actions/core');
const host = core.getInput('host')
const username = core.getInput('username')
const password = core.getInput('password')
const privateKey = core.getInput('ssh-private-key')
const commands = core.getInput('commands') ? core.getInput('commands') : 'ls -la'
// Configure the connection parameters
let connectionParams = {
host: host,
username: username,
};
if (password) {
connectionParams.password = password
} else {
connectionParams.privateKey = privateKey
}
// Create a new SSH client instance
const sshClient = new Client();
sshClient.on('ready', () => {
console.log('Connected via SSH!');
execute()
});
sshClient.on('error', (err) => {
console.error('Error connecting via SSH:', err);
});
function execute() {
if (!commands) {
console.log('Connect SSH closed');
sshClient.end();
return
}
sshClient.exec(commands, (err, stream) => {
if (err) {
sshClient.end();
throw err;
}
stream
.on('close', (code, signal) => {
console.log('Command execution closed');
sshClient.end();
})
.on('data', (data) => {
console.log('Command output:', data.toString());
})
.stderr.on('data', (data) => {
console.error('Command error:', data.toString());
});
});
}
// Connect to the SSH server
sshClient.connect(connectionParams);