【问题标题】:shell commands via ssh2 nodejs通过 ssh2 nodejs 的 shell 命令
【发布时间】:2014-06-25 13:48:38
【问题描述】:

我正在尝试使用 ssh2(https://www.npmjs.org/package/ssh2) 做一些基本的事情,但我没有得到任何结果,而且 api 也不是那么容易理解(对我来说),我正在尝试执行基本的 shell 命令,比如 ls ,pwd .. 但我没有结果。我试图通过 shell 获取 ls -lah,

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

    });

});

有人可以指导我我做错了什么或它应该如何工作吗? 顺便说一句,没有连接问题。

谢谢

【问题讨论】:

    标签: node.js shell ssh


    【解决方案1】:

    这就是你想要的:

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

    【讨论】:

      【解决方案2】:

      在 npm 中使用新版本的 ssh2 (0.3.6),它现在略有变化:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-28
        • 2019-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-04
        • 1970-01-01
        相关资源
        最近更新 更多