Skip to content

Commit

Permalink
Implemented SSH port forwarding and rsync for directory sync
Browse files Browse the repository at this point in the history
  • Loading branch information
bhatiadheeraj committed Nov 13, 2023
1 parent a6ab595 commit ff48c1b
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions bin/transferhop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { Client } = require('ssh2');
const scpClient = require('scp2');

const ssh = new Client();

const fs = require('fs');
const child_process = require('child_process');

const privateKey = 'path/to/your/key.rsa';

const sshConfigIntermediate = {
// name: 'prod.brainlife.io',
host: '149.165.152.60',
username: 'ubuntu',
privateKey: require('fs').readFileSync(privateKey),
// passphrase: 'your key passphrase',
}

const destinationServerDetails = {
remoteHost: '10.0.18.41',//'prod-api-1',
remotePort: 22,
remoteUsername: 'ubuntu',
remotePath: '/tmp/test/'
};

const sourceServerDetails = {
localPath: '/tmp/test/',
localPort: 50000 // This is the port that you are forwarding to the destination server
}

console.log(`Looking for key in: ${privateKey}`, fs.existsSync(privateKey));
console.log(`Current working directory: ${process.cwd()}`);

// ssh -L 50000:prod-api-1:22 [email protected] -fN
// rsync -azv -e 'ssh -p 50000' /tmp/test/file.txt ubuntu@localhost:/tmp/test/file.txt


if (!fs.existsSync(privateKey)) {
console.error('Private key file not found');
} else {
ssh.on('ready', () => {
console.log('SSH Client Ready');

ssh.forwardOut('127.0.0.1', sourceServerDetails.localPort, destinationServerDetails.remoteHost,
destinationServerDetails.remotePort, (err, stream) => {
if (err) {
console.error('Error: ', err);
}

console.log('Forwarding connection established');
const rsyncCommand = `rsync -azv -e 'ssh -p ${sourceServerDetails.localPort}' ${sourceServerDetails.localPath} [email protected]:${destinationServerDetails.remotePath}`;

child_process.exec(rsyncCommand, (err, stdout, stderr) => {
if (err) {
console.error('Error: ', err);
}

console.log('stdout: ', stdout);
console.log('stderr: ', stderr);

ssh.end();
});
});
}).connect(sshConfigIntermediate);
}

0 comments on commit ff48c1b

Please sign in to comment.