Pergunta

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

Foi útil?

Solução

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);
    });
  });
});

Outras dicas

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);
    });
  });
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top