A lightweight SSH Client for Node.js.
NodeSSH is a SSH client for node.js. I would call it lightweight, but since it spawns subprocesses, this makes it heavyweight to me. It uses OpenSSH and expect. Make sure you have installed this software.
Use your system's built-in package manager (apt-get,yum,rpm,dselect,etc..) to install expect on you machine. Most systems (OSX included) should already have this installed. This will not work with windows machines.
Download and install dependencies
$ npm install
var options = { host:"localhost", username:"USER", password:"PASS" };
var SSHClient = require("NodeSSH")(options);
SSHClient.on("connected",function (host) {
SSHClient.exec("pwd",function(data,lastChunk){
console.log("Server response:",data);
if (lastChunk) SSHClient.close();
});
})
SSHClient.connect();
More samples at samples/
directory.
Parameters:
- options - **Type:**OptionObject - **Description:**Options Object - REQUIRED
- options.host - **Type:**string - **Description:**SSH host to be connected - REQUIRED
- options.username - **Type:**string - **Description:**SSH username to be authenticated - REQUIRED
- options.password - **Type:**string - **Description:**SSH user password OR if certificate is specified is the password used to open certificate. - OPTIONAL
- options.certificate - **Type:**string - **Description:**Local SSH key path to authenticate with remote host - OPTIONAL
- options.port - **Type:**integer - **Description:**Remote host SSH port - OPTIONAL (Default is 22)
- options.colors - **Type:**boolean - **Description:**Use or NOT SSH color syntax - OPTIONAL (Default is false)
Sample:
var options = { host:"localhost", username:"USER", password:"PASS", port: 20890 };
var SSHClient = require("NodeSSH")(options);
SSHClient.on("connected",function (host) {
SSHClient.close();
})
SSHClient.connect();
This function will try to connect to remote host.
Sample:
SSHClient.connect();
This function will not call error listener, it'll call 'output'/'input'/'stderr' events.
Parameters:
- command - **Type:**string - **Description:**Command to be write on remote host. - REQUIRED
Sample:
SSHClient.write("pwd");
Execute command on remote connection.
Sample:
SSHClient.on("connected",function (host) {
SSHClient.exec("pwd",function(data,finished){
if (finished) { SSHClient.close(); }
});
});
This function will pause child stdout stream (if not paused).
Since any command executed in the paused session will be processed by the same child (already paused), if you do not pause it, no commands will be executed until.
Sample:
SSHClient.pauseOutput();
This function will resume child stdout stream (if paused).
Sample:
SSHClient.resumeOutput();
This method will close SSH session.
Sample:
SSHClient.close();
####Connected
This event will be fired when SSH Session has being established. You must execute .connect()
function to start connection process.
Event-String: connected
Sample:
//Connections events
SSHClient.on("connected",function (host) {
console.log("Connected to host:",host);
});
####Closed
This event will be fired when SSH Session has being closed (with success or not).
If all goes without errors, you must execute .close()
function to close connection session.
Event-String: closed
Sample:
//Connections events
SSHClient.on("closed",function (host,error) {
if (err) {
console.log("Error on session",err);
}else {
console.log("Disconnected from host:",host);
}
});
####Output This event will be fired when any command is outputed by remote connection (even ours that is outputed by ssh session).
Event-String: output
Sample:
//Command events
SSHClient.on('output', function(data,finished) {
if (!finished) {
console.log("output-> " data);
}else { SSHClient.close(); }
});
####StdErr This event will be fired when any errored command is outputted by remote connection.
Event-String: stderr
Sample:
//Command events
SSHClient.on('stderr', function(err) {
console.log("stderr-> " err);
});
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request