-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex copy.js
58 lines (46 loc) · 1.41 KB
/
index copy.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
require('dotenv').config();
const { exec } = require('child_process');
const vmUsername = process.env.VM_USERNAME;
const vmPassword = process.env.VM_PASSWORD;
const vmIP = process.env.VM_IP;
const vmSSH = process.env.VM_SSH;
// console.log('VM_USERNAME:', vmUsername);
// console.log('VM_PASSWORD:', vmPassword);
// console.log('VM_IP:', vmIP);
// console.log('VM_SSH:', vmSSH);
const fs = require('fs');
const { Client } = require('ssh2');
const remotePath = '/var/lib/asterisk/sounds/en/'; // Source path on your Linux VM
const localPath = 'C:/MovedFiles'; // Destination path in your local machine
// Specify the filename you want to copy from the remotePath
const filename = 'StarWars.wav';
const conn = new Client();
conn.on('ready', () => {
conn.sftp((err, sftp) => {
if (err) {
console.error(`SFTP Error: ${err.message}`);
conn.end();
return;
}
const sourcePath = remotePath + filename;
const destinationPath = localPath + '/' + filename;
sftp.fastGet(sourcePath, destinationPath, (err) => {
if (err) {
console.error(`File transfer error: ${err.message}`);
conn.end();
return;
}
console.log('File copied successfully!');
conn.end();
});
});
});
conn.on('error', (err) => {
console.error(`SSH connection error: ${err.message}`);
});
conn.connect({
host: vmIP,
port: 22,
username: vmUsername,
password: vmPassword,
});