-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.js
More file actions
57 lines (48 loc) · 1.94 KB
/
Copy pathssh.js
File metadata and controls
57 lines (48 loc) · 1.94 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
const { execSync } = require('child_process');
const fs = require('fs');
const readline = require('readline');
const config = require('./config')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the hostname: ', (hostname) => {
if (config[hostname]) {
const targetIP = config[hostname].ip;
const targetMAC = config[hostname].mac;
console.log(`Selected hostname: ${hostname}, IP: ${targetIP}, MAC: ${targetMAC}`);
const sshUser = 'dave'; // Replace with your username
const sshPrivateKey = process.env.SSH_PRIVATE_KEY;
// Check if the private key file exists
if (!fs.existsSync(sshPrivateKey)) {
console.log(`Private key file not found: ${sshPrivateKey}`);
process.exit(1);
}
// Function to wake up the remote PC using Wake-on-LAN
function wakeUpPC() {
execSync(`wakeonlan ${targetMAC}`);
}
// Check if the PC is online by pinging it
let pingResult;
try {
pingResult = execSync(`ping -c 1 ${targetIP}`).toString();
if (pingResult.includes('1 received')) {
// The PC is already awake, so we can SSH into it directly
execSync(`ssh -i ${sshPrivateKey} ${sshUser}@${targetIP}`, { stdio: 'inherit' });
}
else {
console.error('Error occurred while pinging:', error.message);
process.exit(1)
}
} catch (error) {
// The PC is in suspend mode, so wake it up using WoL and then SSH into it
wakeUpPC();
setTimeout(() => {
execSync(`ssh -i ${sshPrivateKey} ${sshUser}@${targetIP}`, { stdio: 'inherit' });
}, 10000); // Adjust the sleep time based on your PC's wake-up time
}
} else {
console.log('Hostname not found in config');
}
rl.close();
});