简体   繁体   English

NodeJs套接字连接问题

[英]NodeJs Socket Connection Issue

I am trying to connect to a process running on a remote machine. 我正在尝试连接到在远程计算机上运行的进程。 I have used ssh2 to connect to the remove machine. 我已经使用ssh2连接到删除计算机。 Now i am trying to connect to a process running in the machine. 现在,我试图连接到计算机中运行的进程。 The process name is dd_servicesc 进程名称为dd_servicesc

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

I used the above code command to connect to the process. 我使用上面的代码命令连接到进程。 On connection i want to execute a series of connections. 在连接上,我想执行一系列连接。 But the currently problem is if i use c.exec again the next command executes out side the process and hence returns permission denied. 但是当前的问题是,如果我再次使用c.exec,那么下一个命令将在进程之外执行,并因此导致拒绝权限。 TO understand better please check below 为了更好地理解,请检查下面

  OmneBabble dd_servicesc
  >0 1 om_get_user_info 31003 1 MACLEAN1-11365

Here the initial command is OmneBabble dd_servicesc within which 0 1 om_get_user_info 31003 1 MACLEAN1-11365 needs to be executed. 此处的初始命令是OmneBabble dd_servicesc,其中需要执行0 1 om_get_user_info 31003 1 MACLEAN1-11365。 Please point out any links which i can refer to find a solution to this problem. 请指出我可以参考的任何链接以找到此问题的解决方案。

Regards, 问候,

Maclean 麦克林

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;

Update: Here OmneBabble dd_services which make a connection to the socket ie> dd_services. 更新:这里OmneBabble dd_services建立到套​​接字的连接,即dd_services。

After that we will publish a request to socket using 0 1 om_get_user_info 31003 1 MACLEAN1-11365 之后,我们将使用0 1 om_get_user_info 31003 1 MACLEAN1-11365向套接字发布请求

Socket will then then publish the request to all its client process. 然后,套接字将请求发布到其所有客户端进程。 Finally a Client process which accepts the request will respond with the output. 最后,接受请求的客户进程将以输出响应。 SO i cannot use child process to map the output of the parent process to child. 所以我不能使用子进程将父进程的输出映射到子进程。

Not sure exactly how you want to connect to the processes, but if the process itself are started via node , you could instead start them with child_process.spawn , where you get an object back and can set the stdout , stderr , and stdin , as well as send signals to the process. 不确定要如何连接到进程,但是如果进程本身是通过node启动的,则可以使用child_process.spawn来启动它们,在其中您可以返回对象并可以将stdoutstderrstdin为以及向过程发送信号。

EDIT: 编辑:

Looking at your attached code, it seems like you want to send your second command to the process via stdin, rather than executing another command. 查看您的附加代码,似乎您想通过stdin将第二条命令发送到进程,而不是执行另一条命令。 You might be able to pipe your second command into your main process as the initial exec: 您也许可以将第二个命令作为初始exec传递到主进程中:

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

i have been able to run an interactive shell with ssh2 我已经能够使用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);

Try inserting the STDIN redirection after your exec statement. 尝试在exec语句之后插入STDIN重定向。

I learned how to redirect STDIN from this closed issue: https://github.com/mscdex/ssh2/issues/94 我了解了如何从已关闭的问题重定向STDIN: https : //github.com/mscdex/ssh2/issues/94

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM