by Logo Technical Blog – Future Processing
24.03.2014
Useful Node.js modules – ssh2_

I would like to introduce a very useful Node.js module to you, which can help in automating your daily work or accomplishing non-ordinary and non-obvious task such as SFTP/SSH management.

Description

ssh2 is a SSH2 client module written in pure JavaScript.

In this case the above mentioned module helped me to bootstrap the simple tool a while ago. The main goal of this tool is to automate creating, downloading and uploading a backup to SFTP for one-of-the-most-popular-services-for-project-management, which in the most basic 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 used a node-chimera module and for the second part (uploading previously downloaded ZIP archive to two different SFTP servers) I used the ssh2 module.

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

API

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

 var fs = require("fs"),
    util = require("util"),
    domain = require("domain"),

    Connection = require("ssh2"),

    OptionsForSFTP = {
      host: "X.Y.Z.U",
      port: 22,
      username: "backup",
      // password: "password";
      privateKey: fs.readFileSync("/path/to/privateKey")
    };

// ...

function uploadToSftpServer(options, fileName, callback) {
  var connection = new Connection(),
      handler = domain.create();

  handler.on("error", function (error) {
    console.error("Error occurred: %s", error);
    process.exit(-1);
  });

  // Handling "error" event inside domain handler.
  handler.add(connection);

  connection.on("ready", function () {
    connection.sftp(handler.intercept(function (sftp) {
      var providedFileName = util.format("./%s", fileName);

      sftp.fastPut(providedFileName, providedFileName,
                   handler.intercept(connection.end.bind(connection)));
    }));
  });

  connection.on("end", callback);
  connection.connect(options);
}

When connection is ready we need to open a SFTP channel and PUT file to the server. As you can see in the 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, by bug fixing and merging pull requests the author incrementally improves design and code quality.
  • It is supported by Node.js version 0.8 and higher (probably also by 0.11 because it does not have any kind of binary extensions).
  • All dependencies are up to date (https://david-dm.org/mscdex/ssh2).
  • Module is used in 75 other modules (https://www.npmjs.org/browse/depended/ssh2).

Quality

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

  • It does not have any kind of tests (unit or even integration tests).
  • Documentation is delivered inside single README.md file in a form of source code examples.
  • Overall code quality is not too high, module still makes an impression of hacked in the hurry, but it just works.

References

1. ssh2 github page – https://github.com/mscdex/ssh2
2. Original post – http://www.afronski.pl/2014/02/04/ssh2-nodejs-module-of-the-week.html

Related Posts

Comments

Cookies

This website stores cookies on your computer. These cookies are used to improve our website and provide more personalized services to you, both on this website and through other media. To find out more about the cookies we use, see our Cookies policy.