rmtcmd
v0.3.0
Published
execute commands local and remotely over SSH
Downloads
523
Readme
rmtcmd
simple execute commands local and remotely over SSH
./example.js -u userName -h 192.168.11.1
✔ [sudo] Password: … ****
#!/usr/bin/env node
const rmtcmd = require('rmtcmd')
// execute commands
const deploy = async ({ config, local, remote }) => {
await local('ls -la ./dist', { cwd: __dirname })
const target = `/home/${config.username}/rmtcmd`
await remote(`sudo mkdir -p ${target}`)
await remote(`sudo chmod 775 ${target}`)
await remote(`sudo chown ${config.username}:${config.username} ${target}`)
const src = path.join(__dirname, 'dist', 'src')
await local(
[
`rsync -av`,
`--exclude='node_modules'`,
`-e 'ssh -i ${config.privateKeyPath}'`,
`${src}/`,
`${config.username}@${config.host}:${target}`
].join(' '),
{
cwd: __dirname
}
)
await remote(`ls -la ${target}`)
}
;(async () => {
const { host, username, privateKeyPath, sudoPassword } = await rmtcmd.cli.getArgs()
const privateKey = require('fs').readFileSync(privateKeyPath)
await rmtcmd.connect({
host,
username,
privateKey,
sudoPassword,
task: deploy
})
})().catch(console.error)
API
connect(config)
wrapper ssh2.client.connect
- config
- ...ssh2.Client.connect
- sudoPassword <string>
- privateKeyPath: <string> ssh private key path (optional)
- task <TaskFunction>
TaskFunction
async function
args
returns Promise<void>
LocalCmd
wrapper child_process.exec
args
- cmd <string> command
- options
- ...
child_process.exec.options
- encoding
- default: 'utf8'
- timeout
- default: 10000
- stdout
- WritableStream
- default: process.stdout
- ...
returns Promise<{ stdout, stderr }>
RemoteCmd
args
cmd <string> command
options
- stdout
- WritableStream
- default: process.stdout
- stderr
- WritableStream
- default: process.stderr
- stdout
returns Promise<{ code: number, signal: boolean, stdout: string }>
cli
getArgs()
- returns
- host <string> remote host.
- option:
-h
or--host
- option:
- port <number> ssh port
- option:
--port
(optional) - default: 22
- option:
- username <string> ssh user name.
- option:
-u
or--username
(optional) - default:
whoami
- option:
- sudoPassword <string> ssh sudo password.
- option:
--sudo-password
(optional) - default: prompt
- option:
- privateKeyPath <string> private key path.
- option:
-k
or--private-key-path
. (optional) - default: ~/.ssh/id_rsa
- option:
- host <string> remote host.