NodeJs 套接字连接问题

NodeJs Socket Connection Issue

本文关键字:问题 连接 套接字 NodeJs      更新时间:2023-10-04

我正在尝试连接到远程计算机上运行的进程。我已经使用 ssh2 连接到删除机器。现在我正在尝试连接到机器中运行的进程。进程名称为 dd_servicesc

c.exec('OmneBabble dd_servicesc',function(){}

我使用上面的代码命令连接到进程。在连接上,我想执行一系列连接。但是目前的问题是,如果我再次使用 c.exec,下一个命令会在进程之外执行,因此返回权限被拒绝。为了更好地理解,请查看以下内容

  OmneBabble dd_servicesc
  >0 1 om_get_user_info 31003 1 MACLEAN1-11365

这里的初始命令是 OmneBabble dd_servicesc,其中需要执行 0 1 om_get_user_info 31003 1 MACLEAN1-11365。请指出我可以参考的任何链接,以找到解决此问题的方法。

问候

麦克莱恩

Update

function ssh()
{
var Connection = require('ssh2');
var c = new Connection();
c.on('connect', function() {
  console.log('Connection :: connect');
});
c.on('ready', function() {
  console.log('Connection :: ready');
c.exec('OmneBabble dd_servicesc', function(err, stream) {
    if (err) throw err;
    stream.on('data', function(data, extended) {
      console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ')
                  + data);
    });
    stream.on('end', function() {
      console.log('Stream :: EOF');
    });
    stream.on('close', function() {
      console.log('Stream :: close');
    });
    stream.on('exit', function(code, signal) {
      console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
      c.end();
    });
  });
c.exec('0 1 om_get_user_info 31003 1 MACLEAN1-11365', function(err, stream) {
    if (err) throw err;
    stream.on('data', function(data, extended) {
      console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ')
                  + data);
    });
    stream.on('end', function() {
      console.log('Stream :: EOF');
    });
    stream.on('close', function() {
      console.log('Stream :: close');
    });
    stream.on('exit', function(code, signal) {
      console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
      c.end();
    });
  });
});
c.on('error', function(err) {
  console.log('Connection :: error :: ' + err);
});
c.on('end', function() {
  console.log('Connection :: end');
});
c.on('close', function(had_error) {
  console.log('Connection :: close');
});
c.connect({
  host: '192.168.20.204',
  port: 22,
  username: 'oaa',
  password: 'marigold'
});
}
exports.ssh = ssh;

更新:在这里 OmneBabble dd_services连接到套接字,即> dd_services。

之后,我们将使用 0 1 om_get_user_info 31003 1 MACLEAN1-11365 发布对套接字的请求

然后,套接字会将请求发布到其所有客户端进程。最后,接受请求的客户端进程将使用输出进行响应。所以我不能使用子进程将父进程的输出映射到子进程。

不确定如何

连接到进程,但如果进程本身是通过node启动的,你可以用child_process.spawn启动它们,在那里你得到一个对象,可以设置stdoutstderrstdin,以及向进程发送信号。

编辑:

查看附加的代码,似乎您希望通过 stdin(标准(将第二个命令发送到进程,而不是执行另一个命令。您也许可以将第二个命令作为初始可执行文件通过管道传输到主进程中:

c.exec('echo "0 1 om_get_user_info 31003 1 MACLEAN1-11365" | OmneBabble dd_servicesc', function...

我已经能够用 ssh2 运行交互式 shell

c.shell(function(err, stream) {
if (err) throw err;
 stream.on('data', function(data, extended) {

  console.log((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ')
            + data);
}); 
// below code to connect stdin to the remote shell 
process.stdin.resume();
process.stdin.on('data', function (data) {
allowed = false;
stream.write(data);

尝试在 exec 语句后插入 STDIN 重定向。

我学会了如何从这个已关闭的问题中重定向 STDIN:https://github.com/mscdex/ssh2/issues/94