簡體   English   中英

節點js的ssh2中的客戶端方法“exec”是否有同步等效項?

[英]Is there a synchronous equivalent for the Client method "exec" in ssh2 for node js?

我已經在節點 js 中使用 ssh2 連接到遠程機器。
現在我想在遠程機器上同步運行命令。

var Client = require('ssh2').Client;


var conn = new Client();
conn.on('ready', function() {
  console.log('Client :: ready');
  conn.exec('uptime', function(err, stream) {
    if (err) throw err;
    stream.on('close', function(code, signal) {
      console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
      conn.end();
    }).on('data', function(data) {
      console.log('STDOUT: ' + data);
    }).stderr.on('data', function(data) {
      console.log('STDERR: ' + data);
    });
  });
}).connect({
  host: '192.168.100.100',
  port: 22,
  username: 'frylock',
  privateKey: require('fs').readFileSync('/here/is/my/key')
});

這樣我可以以異步方式執行它。 如果有任何 function 等效於“exec”,即同步。

使用 conn.shell 代替 conn.exec。 使用 shell 方法,您可以啟動交互式 shell session。

這里有一個例子:

https://github.com/mscdex/ssh2#start-an-interactive-shell-session

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM