Ssh with nodejs child_process, command not found on server

I am trying to run a simple ssh command through nodejs child_process. when I run the command through nodejs code, it does not respond that the command that I sent to the server was not found. when I run the same command that just copies and pastes into my terminal window, it works fine.

here is the command line version I'm trying to do:

ssh user@example.com 'ls -lai'

and here is the nodejs version of the same ssh command using child_process

var cproc = require('child_process');
var exec = cproc.exec;
var spawn = cproc.spawn;

var command = "ssh";
var args = ["user@example.com", "'ls -lai'"];

var child = spawn(command, args);

child.stdout.on('data', function(data) {
  console.log('stdout: ' + data);
});

child.stderr.on('data', function(data) {
  console.log('stderr: ' + data);
});

child.on('close', function(code) {
  console.log('exit code: ' + code);
  process.exit();
});

the output from the command line version is exactly what I expect ... I get a list of directories. but when I run this nodejs code to execute the same command, the stderr callback code is run and the command returns code 127 (command not found).

$ node test-ssh.js 
stderr: bash: ls -lai: command not found

exit code: 127

ls -lai ... , , .

- , ssh nodejs , ?

+4
2

, .

var args = ["user@example.com", "ls -lai"];

+2

, , - , , .

:

var args = ["user@example.com", "'cd ~ && ls -lai'"]

. , , SSH, child_process :

var args = ["user@example.com", "cd ~ && ls -lai"]
0

Source: https://habr.com/ru/post/1569154/


All Articles