Question

I am wondering is it possible to run methods provided in node-ssh2 in a blocking way.

I am testing my code with node-vows.

snippet of conn-test.js

suite = vows.describe("conn-test");
suite.addBatch({
    topic: function () {
         return new Connection("1.2.3.4", "root", "oopsoops");
    }
    "run ls": function (conn) {
         conn.send("ls");
    }
});

snippet of conn.js

var ssh2 = require("ssh2");
function Connection(ip, user, pw) {
    //following attributes will be used on this.send()
    this.sock = new ssh2();
    this.ip = ip;
    this.user = user;
    this.pw = pw;
}
Connection.prototype.send = function (msg) {
    var c = this.sock;
    //Copy the example 1 on https://github.com/mscdex/ssh2
}

Node-Vows runs my code without errors. However, the problem is vows terminated faster than callback from ssh2. In other word, I cannot get response from ssh2.

It seems that node-async is one of the possible solution. However, I have no idea how to force the event driven calls becomes a blocking call by the help of async.

Anyone can help?

--Updated 10/04/2014

Fix the typo on title....

Was it helpful?

Solution

I've never used vows before, but according to their reference documentation, you should use this.callback instead of returning a value. Your vows code might instead look something like:

var ssh2 = require('ssh2'),
    assert = require('assert');
suite = vows.describe('conn-test');
suite.addBatch({
  topic: function() {
    var conn = new ssh2.Connection(),
        self = this;
    conn.connect({
      host: '1.2.3.4',
      port: 22,
      username: 'root',
      password: 'oopsoops'
    });
    conn.on('ready', function() {
      self.callback(conn);
    });
  },
  'run ls': function(conn) {
    var self = this;
    conn.exec('ls', function(err, stream) {
      assert(!err);
      stream.on('exit', function(code, signal, coredump) {
        assert(code === 0);
        assert(!signal);
        assert(!coredump);
        self.callback();
      });
    });
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top