simple-ssh
A wrapper for the ssh2 client module by Brian White which makes it easier to run a sequence of commands over SSH.
Requirements
Install
npm install simple-ssh
Examples
- Echoing out a users
PATH
:
var SSH = require('simple-ssh');
var ssh = new SSH({
host: 'localhost',
user: 'username',
pass: 'password'
});
ssh.exec('echo $PATH', {
out: function(stdout) {
console.log(stdout);
}
}).start();
ssh.exec('echo', {
args: ['$PATH'],
out: function(stdout) {
console.log(stdout);
}
}).start();
ssh.exec('this-does-not-exist', {
err: function(stderr) {
console.log(stderr);
}
}).start();
ssh.exec('exit 69', {
exit: function(code) {
console.log(code);
}
}).start();
- Chaining commands together:
ssh
.exec('echo "Node.js"', {
out: console.log
})
.exec('echo "is"', {
out: console.log
})
.exec('echo "awesome!"', {
out: console.log
})
.start();
- Running a command using
sudo
ssh.exec('sudo echo "Pseudo-sudo"', {
pty: true,
out: console.log
}).start();
API
Functions
-
Constructor( [ config ] )
- config { Object }:
- config.host { String }: Hostname
- config.port { Number }: Port number (default:
22
) - config.user { String }: Username
- config.pass { String }: Password
-
exec( command, [ options ] ): Adds a command to the stack
- command { String }: Command to be executed
- options { Object }:
- options.args { String[] }: Additional command line arguments (default:
null
) - options.out { Function( stdout ) }:
stdout
handler
- stdout { String }: Output streamed through
stdout
- options.err { Function( stderr ) }:
stderr
handler
- stderr { String }: Output stream through
stderr
- options.exit { Function( code ) }: Exit handler
- code { Number }: Exit code
- options.pty { Boolean }: Allocates a pseudo-tty, useful for command which require
sudo
(default: false
)
-
start( [ options ] ): Starts executing the commands
- options { Object }:
- options.success { Function() }: Called on successful connection
- options.fail { Function(err) }: Called if the connection failed
- err { Error }: Error information
-
end() ** Ends the SSH session**