-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (42 loc) · 1.31 KB
/
app.js
File metadata and controls
54 lines (42 loc) · 1.31 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
const { readFileSync } = require('fs');
const { Client } = require('ssh2');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Create a new SSH client instance
const sshClient = new Client();
// Configure the connection parameters
const connectionParams = {
host: 'your remote server host',
username: 'your username',
privateKey: readFileSync('path/to/private/key')
};
// Connect to the SSH server
sshClient.connect(connectionParams);
// Handle events when the connection is established
sshClient.on('ready', () => {
console.log('Connected via SSH!');
// Prompt the user to enter a command
rl.question('Enter a command to execute on the remote server: ', (command) => {
ececute(command)
});
});
function ececute(command){
// Execute the user-entered command on the remote server
sshClient.exec(command, (err, stream) => {
if (err) throw err;
stream.on('close', (code, signal) => {
console.log('Command execution closed');
sshClient.end();
rl.close();
})
.on('data', (data) => {
console.log('Command output:', data.toString());
})
.stderr.on('data', (data) => {
console.error('Command error:', data.toString());
});
});
}