This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hsiaoming Yang
committed
Mar 5, 2013
1 parent
6fa9e7f
commit d4fa388
Showing
6 changed files
with
284 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
#!/usr/bin/env node | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var client = require('../'); | ||
|
||
main(process.argv.slice()); | ||
|
||
function main(argv) { | ||
var identify, src, dest; | ||
|
||
var quiet = false; | ||
var port = 22; | ||
var defaults = {}; | ||
|
||
var getArg = function() { | ||
var args = argv.shift(); | ||
args = args.split('='); | ||
if (args.length > 1) { | ||
argv.unshit(args.slice(1).join('=')); | ||
} | ||
return args[0]; | ||
} | ||
|
||
var arg, remains = []; | ||
|
||
while (argv.length) { | ||
arg = getArg(); | ||
switch(arg) { | ||
case '-p': | ||
case '--port': | ||
port = argv.shift(); | ||
break; | ||
case '-q': | ||
case '--quiet': | ||
quiet = true; | ||
break; | ||
case '-i': | ||
case '--identify': | ||
identify = argv.shift(); | ||
break; | ||
case '-h': | ||
case '--help': | ||
helpMessage(); | ||
break; | ||
default: | ||
remains.push(arg); | ||
break; | ||
} | ||
} | ||
|
||
if (remains.length !== 4) { | ||
helpMessage(); | ||
} | ||
printLog(quiet); | ||
|
||
src = remains[2]; | ||
dest = remains[3]; | ||
|
||
defaults = { | ||
port: parseInt(port, 10) | ||
} | ||
|
||
var password; | ||
var parsed = client.parse(src); | ||
|
||
if (parsed.username && parsed.host && parsed.path) { | ||
password = parsed.password; | ||
} else { | ||
password = client.parse(dest).password; | ||
} | ||
|
||
client.on('error', function(err) { | ||
if (err.code === 'ECONNREFUSED') { | ||
prompt(' password: ', function(val) { | ||
client.close(); | ||
delete client.__ssh; | ||
defaults.password = val; | ||
scp(src, dest, defaults); | ||
}); | ||
} else { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
if (!password && identify && fs.existsSync(identify)) { | ||
defaults.privateKey = fs.readFileSync(identify); | ||
scp(src, dest, defaults); | ||
} else if (!password) { | ||
prompt(' password: ', function(val) { | ||
defaults.password = val; | ||
scp(src, dest, defaults); | ||
}); | ||
} else { | ||
defaults.password = password; | ||
scp(src, dest, defaults); | ||
} | ||
} | ||
|
||
function helpMessage() { | ||
console.log(); | ||
var lines = [ | ||
' Usage:', | ||
' scp2 [-p 22] localfile server', | ||
'', | ||
' Options:', | ||
' -p, --port=<port> ssh port, default: 22', | ||
' -i, --identify=<file> identify file', | ||
' -q, --quiet do not show log', | ||
' -h, --help display this message', | ||
'', | ||
' Examples:', | ||
' $ scp2 data.txt admin:[email protected]:/home/admin/', | ||
' $ scp2 data.txt [email protected]:/home/admin/rename.txt', | ||
'', | ||
]; | ||
console.log(lines.join('\n')); | ||
process.exit(); | ||
} | ||
|
||
function printLog(quiet) { | ||
if (!quiet) { | ||
client.on('connect', function() { | ||
util.log('connected'); | ||
}); | ||
client.on('ready', function() { | ||
util.log('ready'); | ||
}); | ||
client.on('mkdir', function(p) { | ||
util.log('mkdir ' + p); | ||
}); | ||
client.on('write', function(o) { | ||
util.log('write ' + o.destination); | ||
}); | ||
client.on('close', function() { | ||
util.log('close'); | ||
}); | ||
client.on('end', function() { | ||
util.log('end'); | ||
}); | ||
} | ||
} | ||
|
||
function prompt(str, fn) { | ||
process.stdout.write(str); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.once('data', function(val) { | ||
fn(val.trim()); | ||
}).resume(); | ||
} | ||
|
||
function scp(src, dest, defaults) { | ||
client.defaults(defaults); | ||
client.scp(src, dest, function(err) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} else { | ||
util.log('scp success.'); | ||
} | ||
process.exit(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/scp'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.