简体   繁体   中英

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. Now i am trying to connect to a process running in the machine. The process name is 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. 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. 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.

After that we will publish a request to socket using 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.

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. You might be able to pipe your second command into your main process as the initial 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

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.

I learned how to redirect STDIN from this closed issue: https://github.com/mscdex/ssh2/issues/94

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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