Home

ssh2 - Node.js module of the week

Introduction

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).

Description

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.

API

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"),
 4 
 5     Connection = require("ssh2"),
 6 
 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     };
14 
15 // ...
16 
17 function uploadToSftpServer(options, fileName, callback) {
18   var connection = new Connection(),
19       handler = domain.create();
20 
21   handler.on("error", function (error) {
22     console.error("Error occurred: %s", error);
23     process.exit(-1);
24   });
25 
26   // Handling "error" event inside domain handler.
27   handler.add(connection);
28 
29   connection.on("ready", function () {
30     connection.sftp(handler.intercept(function (sftp) {
31       var providedFileName = util.format("./%s", fileName);
32 
33       sftp.fastPut(providedFileName, providedFileName,
34                    handler.intercept(connection.end.bind(connection)));
35     }));
36   });
37 
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.

Health

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).

Quality

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

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

References

  1. ssh2 github page
Home