2

I'm trying to do basic stuff with ssh2(https://www.npmjs.org/package/ssh2) but I don't get any result and the api ain't that understandable (for me), I'm trying to do basic shell commands like ls,pwd .. but ive no results. I tried this to get ls -lah via shell,

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

    c.shell('ls','lah', function(err,stream) {
    if (err) throw err;

         stream.on('ls', function(data, extended) {
              console.log(data);
              console.log(extended);
         });

    });

});

can someone direct me what im doing wrong or how it supposed to work ? btw there no connection problem.

thanks

2 Answers 2

3

Here's what you want:

c.on('ready', function() {
  c.exec('ls -lah', function(err, stream) {
    if (err)
      throw err; // Do something more sensible than this

    stream.on('data', function(data) {
      console.log('STDOUT: ' + data);
    });
    stream.stderr.on('data', function(data) {
      console.log('STDERR: ' + data);
    });
    stream.on('close', function(code, signal) {
      console.log('Process closed with code ' + code);
    });
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

With the new version of ssh2 (0.3.6) in npm, its slightly changed now:

c.on('ready', function() {
  c.exec('ls -lah', function(err, stream) {
    if (err) throw err;
    stream.on('data', function(data) {
      console.log('STDOUT: ' + data);      
    }).stderr.on('data', function(data){
      console.log('STDERR: ' + data);      
    }).on('exit', function(code, signal) {
      console.log('Exited with code ' + code + ' and signal: ' + signal);
    });
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.