ssh2 - Node.js module of the week


In this series of articles I would like to show interesting, sometimes solid, sometimes just working and really useful Node.js modules which helps me in the last week (or earlier). Each post will describe a reason why I chosen this module, an API description or sample usage. It will also describe health (in other words - what it supports and answer for question “Is it actively developed?”) and quality (documentation, tests and overall code quality).


ssh2 is a SSH2 client module written in pure JavaScript.

In this case mentioned module helps me bootstrap the simple tool a while ago. The main goal of this tool is to automate create, download and upload a backup to SFTP for one-of-the-most-popular-services-for-project-management which in cheapest version does not provide an API for that.

For the first part (UI automation script which will create the backup and download an archive) I have used a node-chimera module (which I will describe in the next blog post) and for second part (uploading previously downloaded ZIP archive to two different SFTP servers) I have used a ssh2 module.


In the most basic case you need only to require this module and create new connection. It is useful to use domains for error handling, but it is not an obligatory practice.

 1 var fs = require("fs"),
 2     util = require("util"),
 3     domain = require("domain"),
 5     Connection = require("ssh2"),
 7     OptionsForSFTP = {
 8       host: "X.Y.Z.U",
 9       port: 22,
10       username: "backup",
11       // password: "password"
12       privateKey: fs.readFileSync("/path/to/privateKey")
13     };
15 // ...
17 function uploadToSftpServer(options, fileName, callback) {
18   var connection = new Connection(),
19       handler = domain.create();
21   handler.on("error", function (error) {
22     console.error("Error occurred: %s", error);
23     process.exit(-1);
24   });
26   // Handling "error" event inside domain handler.
27   handler.add(connection);
29   connection.on("ready", function () {
30     connection.sftp(handler.intercept(function (sftp) {
31       var providedFileName = util.format("./%s", fileName);
33       sftp.fastPut(providedFileName, providedFileName,
34                    handler.intercept(connection.end.bind(connection)));
35     }));
36   });
38   connection.on("end", callback);
39   connection.connect(options);
40 }

When connection is ready we need to open a SFTP channel and PUT file to the server. As you can see in example presented above, authentication via password or private key is a piece of cake and it is handled by module internals.


Module is under active development, bugs are quickly resolved. Besides bug fixing and merging pull requests author incrementally improves design and code quality.

It is supported on Node.js version 0.8 and higher (probably also on 0.11 because it does not have any kind of binary extensions).


Unfortunately module is not an any kind of reference in terms of quality:

But it works and it has just a few bugs :wink:.


  1. ssh2 github page