mikeal / sequest

Simplified API for SSH and SFTP similar to request.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to know if it failed?

jdarling opened this issue · comments

Given a bad connection string there doesn't seem to be any feedback that Sequest has failed. Is there a way to tell what went wrong and where? I read the readme, but don't see it in there.

EG 1:

var sequest = require('sequest');

var seq = sequest('nouser@invalidhost');

seq.pipe(process.stdout); // only necessary if you want to see the output in your terminal
seq.write('ls -la');
seq.write('touch testfile');
seq.write('ls -la');
seq.end();

EG 2:

var sequest = require('sequest');

var seq = sequest('root@127.0.0.1');

seq.pipe(process.stdout); // only necessary if you want to see the output in your terminal
seq.write('ls -la');
seq.write('invalidcommand testfile');
seq.write('ls -la');
seq.end();

Seems there should be an error callback someplace that returns the error type (connection refused, invalid login, bad command) and the details.

@jdarling when its used as a stream, you can listen to the on('error') event to catch any of those cases. Otherwise the error is captured and sent to the callback when using it with the other interface.

By chance can you post an example of that? I'm actually looking at streaming this back into a web socket for remote access.

@jdarling no different from the example you gave except make sure to tack on

seq.on('error', function (err) {
  //handle me
});

And then you would just swap process.stdout for your websocket writeable stream which you can get with primus or websocket-stream

I think I finally found my typo. I was trying to use the "Continuous Mode" code as is and it never did anything. Looked at the test cases though and noticed that I needed to basically get the object back from the connection. Below code works:

// Require sequest so we can use it
var sequest = require('sequest');

// Setup the host to connect to
var host = 'root@127.0.0.1';

// Create the connection
var conn = sequest.connect(host);

// Get the sequest object back for streaming and error handling
var seq = conn();

// Pipe output to the terminal, substitute for whatever stream you want
// or remove completely if you don't need it
seq.pipe(process.stdout);

seq.on('error', function (err) {
  // Error handler goes here
  throw err;
});

// Do some stuff
seq.write('ls -la');
seq.write('invalidcommand testfile');
seq.write('ls -la');

// All done
seq.end();

This code didn't work:

// Require sequest so we can use it
var sequest = require('sequest');

// Setup the host to connect to
var host = 'root@127.0.0.1';

// Create the connection and get the sequest object back
var seq = sequest(host);

// Pipe output to the terminal, substitute for whatever stream you want
// or remove completely if you don't need it
seq.pipe(process.stdout);

seq.on('error', function (err) {
  // Error handler goes here
  throw err;
});

// Do some stuff
seq.write('ls -la');
seq.write('invalidcommand testfile');
seq.write('ls -la');

// All done
seq.end();

Just posting in case someone else hits the same mis-understanding :). Close this if you would like. I think you should add something like the above to the documentation though. But, if the 2nd one should have worked, it didn't.

I'm on Windows connecting to a Linux host.

Oh, and thanks for your prompt replies. Must faster than I manage to be with mine :)

thanks @jdarling !! i have the same problem, now is working!!