简体   繁体   English

节点js的ssh2中的客户端方法“exec”是否有同步等效项?

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

I have connected to a remote machine using ssh2 in node js.我已经在节点 js 中使用 ssh2 连接到远程机器。
Now I want to run commands on the remote machine synchronously.现在我想在远程机器上同步运行命令。

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

This way i can execute it in an async way.这样我可以以异步方式执行它。 If there any function equivalent to "exec" which is sync.如果有任何 function 等效于“exec”,即同步。

Use conn.shell instead of conn.exec.使用 conn.shell 代替 conn.exec。 With shell method you can start an interactive shell session.使用 shell 方法,您可以启动交互式 shell session。

Here an example:这里有一个例子:

https://github.com/mscdex/ssh2#start-an-interactive-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