-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 8cb5c5f
Showing
104 changed files
with
42,058 additions
and
0 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,4 @@ | ||
##Nodejs pia sshexe cli: | ||
|
||
###Example | ||
bin/pia_ssh qiu:123456@172.16.149.129:/home/qiu/test/ 'ls -a' |
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,209 @@ | ||
#!/usr/bin/env node | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
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 '-v': | ||
case '--version': | ||
versionMessage(); | ||
break; | ||
case '-h': | ||
case '--help': | ||
helpMessage(); | ||
break; | ||
default: | ||
remains.push(arg); | ||
break; | ||
} | ||
} | ||
|
||
if (remains.length !== 4) { | ||
helpMessage(); | ||
} | ||
//printLog(quiet); | ||
|
||
dest = remains[2]; | ||
command = remains[3]; | ||
|
||
defaults = { | ||
port: parseInt(port, 10) | ||
}; | ||
|
||
var password; | ||
var parsed = client.parse(dest); | ||
|
||
if (parsed.username && parsed.host && parsed.path) { | ||
password = parsed.password; | ||
} | ||
|
||
client.on('error', function(err) { | ||
if (err.code === 'ECONNREFUSED') { | ||
prompt(' password: ', function(val) { | ||
client.close(); | ||
delete client.__ssh; | ||
defaults.password = val; | ||
pia_ssh(dest,command,defaults); | ||
}); | ||
} else { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
if (!password && identify && fs.existsSync(identify)) { | ||
defaults.privateKey = fs.readFileSync(identify); | ||
pia_ssh(dest,command,defaults); | ||
} else if (!password) { | ||
prompt(' password: ', function(val) { | ||
defaults.password = val; | ||
pia_ssh(dest,command,defaults); | ||
}); | ||
} else { | ||
defaults.password = password; | ||
pia_ssh(dest,command,defaults); | ||
} | ||
} | ||
|
||
function helpMessage() { | ||
var lines = [ | ||
' Usage:', | ||
' pia_ssh [-p 22] serverdir command', | ||
'', | ||
' Options:', | ||
' -p, --port [port] ssh port, default: 22', | ||
' -i, --identify [file] identify file', | ||
' -q, --quiet do not show log', | ||
' -v, --version display the version', | ||
' -h, --help display this message', | ||
'', | ||
' Examples:', | ||
' $ pia_ssh admin:password@example.com:/home/admin/ \'ls -a\'', | ||
'', | ||
]; | ||
console.log(lines.join('\n')); | ||
process.exit(); | ||
} | ||
|
||
function versionMessage() { | ||
var pkg = require('../package.json'); | ||
var format = require('util').format; | ||
console.log() | ||
console.log(format(' %s %s (%s)', pkg.name, pkg.version, process.platform)); | ||
console.log() | ||
process.exit(); | ||
} | ||
|
||
function printLog(quiet) { | ||
if (!quiet) { | ||
client.on('connect', function() { | ||
log('connected'); | ||
}); | ||
client.on('ready', function() { | ||
log('ready'); | ||
}); | ||
client.on('mkdir', function(p) { | ||
log('mkdir ' + p); | ||
}); | ||
client.on('write', function(o) { | ||
log('write ' + o.destination); | ||
}); | ||
client.on('transfer', function(buf, up, total) { | ||
up = up + 1; | ||
log('transfer ' + up + '/' + total + ' data', up < total); | ||
}); | ||
client.on('read', function(p) { | ||
log('read ' + p); | ||
}); | ||
client.on('close', function() { | ||
log('close'); | ||
}); | ||
client.on('end', function() { | ||
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 pia_ssh(dest,command,defaults) { | ||
console.log(); | ||
client.defaults(defaults); | ||
client.pia_ssh(dest, command, function(err) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} else { | ||
log('pia_ssh exec success'); | ||
} | ||
console.log(); | ||
process.exit(); | ||
}); | ||
} | ||
|
||
|
||
function pad(n) { | ||
return n < 10 ? '0' + n.toString(10) : n.toString(10); | ||
} | ||
|
||
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', | ||
'Oct', 'Nov', 'Dec']; | ||
|
||
// 26 Feb 16:19:34 | ||
function timestamp() { | ||
var d = new Date(); | ||
var time = [pad(d.getHours()), | ||
pad(d.getMinutes()), | ||
pad(d.getSeconds())].join(':'); | ||
return [d.getDate(), months[d.getMonth()], time].join(' '); | ||
} | ||
|
||
function grey(msg) { | ||
return ' \x1b[90m' + msg.toString() + '\x1b[39m'; | ||
} | ||
|
||
function log(msg, r) { | ||
msg = grey(timestamp()) + ' - ' + msg.toString(); | ||
process.stdout.write(msg + (r ? '\r' : '\n')); | ||
} |
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/pia_ssh'); |
Oops, something went wrong.