The Node.js ssh2 module to provide a simplify the creation of a new SSH connection. For example, the below code is explained to connect and execute a simple command the destination machine. This can support node.js -- v0.8.7 or newer.


The following modules to be install in your machine,

$ npm install ssh2

$ npm install exec

$ npm install sys

$ npm install child_process

Simple Node.js script:

var Connection = require('ssh2');
var exec = require('child_process').exec;
var sys = require('sys')

var c = new Connection();

c.on('ready', function() {;

var onShell = function(err, stream) { 
    if (err != null) {
        console.log('error: ' + err);
/*We need to give the command what they will execute */

  c.exec("echo 'thelinuxfaq' >> /root/linuxfaq.txt", function (error, stdout, stderr) {
    console.log("Executed command successfully"); 

/*Provide Destination IP Address, SSH port, User name and Password*/
    host: 'Destination_Machine_IP_Address',
    port: 22,
    username: 'root',
    password: 'Password'