Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer files ssh intermediate rsync #55

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions bin/transferhop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { Client } = require('ssh2');

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()}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anibalsolon should I remove both of the logs ?


// ssh -L 50000:prod-api-1:22 ubuntu@prod.brainlife.io -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} ubuntu@127.0.0.1:${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);
}
Loading
Loading