From 45a3ab581743d0e6173c70cc65ef481e8794858e Mon Sep 17 00:00:00 2001 From: tbouchnafa Date: Mon, 7 Aug 2017 12:41:30 +0200 Subject: [PATCH] Add additional ssh options --- README.md | 9 ++++++--- package.json | 2 +- scp.js | 15 +++++++++++++-- test/get.js | 5 ++++- test/send.js | 6 +++++- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 135dfa1..51453c2 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ var options = { user: 'username', host: 'myServer', port: '22', - path: '~' + path: '~', + ssh: ["StrictHostKeyChecking=no", "UserKnownHostsFile=/dev/null"] } scp.send(options, function (err) { @@ -43,7 +44,8 @@ scp.send({ user: 'username', // username to authenticate as on remote system host: 'myServer', // remote host to copy to, set up in your ~/.ssh/config port: '22', // remote port, optional, defaults to '22' - path: '~' // remote path to save to (this would result in a ~/file.txt on myServer) + path: '~', // remote path to save to (this would result in a ~/file.txt on myServer) + ssh: ["StrictHostKeyChecking=no", "UserKnownHostsFile=/dev/null"] // additional ssh options (can be an array of options or one string option) }); ```` @@ -61,7 +63,8 @@ scp.get({ user: 'username', // username to authenticate as on remote system host: 'myServer', // remote host to transfer from, set up in your ~/.ssh/config port: '22', // remote port, optional, defaults to '22' - path: '~' // local path to save to (this would result in a ~/file.txt on the local machine) + path: '~', // local path to save to (this would result in a ~/file.txt on the local machine) + ssh: "StrictHostKeyChecking=no" }); ```` diff --git a/package.json b/package.json index 40f5c2e..0d6e739 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ ], "name": "scp", "description": "remote file copy wrapper", - "version": "0.0.4", + "version": "0.0.5", "repository": { "type": "git", "url": "git://github.com/ecto/node-scp.git" diff --git a/scp.js b/scp.js index 9c78819..21b14bf 100644 --- a/scp.js +++ b/scp.js @@ -6,6 +6,15 @@ var exec = require('child_process').exec; var scp = module.exports = {}; +function buildSshOptions(options) { + result = ''; + var ssh_options = options.ssh_options || {} + ssh_options['ControlMaster'] = 'no' + Object.keys(ssh_options).map(function (key) { + result += "-o " + key + '=' + ssh_options[key] + ' ' + }) + return result.trim(); +} /* * Transfer a file to a remote host */ @@ -15,7 +24,7 @@ scp.send = function (options, cb) { '-r', '-P', (options.port == undefined ? '22' : options.port), - '-o "ControlMaster no"', //callback is not fired if ssh sessions are shared + buildSshOptions(options), options.file, (options.user == undefined ? '' : options.user+'@') + options.host + ':' + options.path, ]; @@ -32,12 +41,14 @@ scp.send = function (options, cb) { * Grab a file from a remote host */ scp.get = function (options, cb) { + var ssh_options = ["ControlMaster=no"]; //callback is not fired if ssh sessions are shared + ssh_options = options.ssh ? ssh_options.concat(options.ssh) : ssh_options; var command = [ 'scp', '-r', '-P', (options.port == undefined ? '22' : options.port), - '-o "ControlMaster no"', //callback is not fired if ssh sessions are shared + buildSshOptions(options), (options.user == undefined ? '' : options.user+'@') + options.host + ':' + options.file, options.path ]; diff --git a/test/get.js b/test/get.js index ee6f9fa..235f370 100644 --- a/test/get.js +++ b/test/get.js @@ -3,7 +3,10 @@ var scp = require('../'); scp.get({ file: '~/test', host: 'core', - path: './test/what' + path: './test/what', + ssh_options: { + StrictHostKeyChecking: 'no' + } }, function () { console.log(arguments); }); diff --git a/test/send.js b/test/send.js index fa414e9..af3db7b 100644 --- a/test/send.js +++ b/test/send.js @@ -3,7 +3,11 @@ var scp = require('../'); scp.send({ file: './test/what', host: 'core', - path: '~' + path: '~', + ssh_options: { + StrictHostKeyChecking: 'no', + UserKnownHostsFile: '/dev/null' + } }, function () { console.log(arguments); });