Useful Node.js modules - ssh2

Useful Node.js modules – ssh2

data: 24 marca, 2014
czas czytania: 3 min
autor: Wojciech Gawroński

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

Newsletter IT leaks

Dzielimy się inspiracjami i nowinkami z branży IT. Szanujemy Twój czas - obiecujemy nie spamować i wysyłać wiadomości raz na dwa miesiące.

Subscribe to our newsletter

Administratorem Twoich danych osobowych jest Future Processing S.A. z siedzibą w Gliwicach. Twoje dane będziemy przetwarzać w celu przesyłania cyklicznego newslettera dot. branży IT. W każdej chwili możesz się wypisać lub edytować swoje dane. Więcej informacji znajdziesz w naszej polityce prywatności.

Subscribe to our newsletter

Administratorem Twoich danych osobowych jest Future Processing S.A. z siedzibą w Gliwicach. Twoje dane będziemy przetwarzać w celu przesyłania cyklicznego newslettera dot. branży IT. W każdej chwili możesz się wypisać lub edytować swoje dane. Więcej informacji znajdziesz w naszej polityce prywatności.