node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

stderr always coming in stdout

Open syedhabib53 opened this issue 4 years ago • 3 comments

Hi,

I have faced an issue, which always returns stderr in result.stdout rather than result.stderr. node-ssh version 10.0.2

ssh.connect({
  host: 'localhost',
  username: '<username>',
  password: '<password>'
}).then(() => {
  ssh.exec('sudo apt updat', [], {
    execOptions: { pty : true },
    stdin: '<password>\n',
    stream: 'both'
  }).then((result) => {
    console.log("Result StdOut", result.stdout)
    console.log("Result StdErr", result.stderr)
    console.log("Result", result)
  })
})

Note: Intensively I make an error in the command sudo apt updat to expecting error.

When I execute above code I got the error always in the stdout.

Result StdOut 
[sudo] password for <username>: 
E: Invalid operation updat
Result StdErr 
Result {
  code: 100,
  signal: null,
  stdout: '<password>\r\n' +
    '[sudo] password for <username>: \r\n' +
    '\u001b[1;31mE: \u001b[0mInvalid operation updat\u001b[0m',
  stderr: ''
}

But the same is working expected using ssh2 Below is the sample.

var Client = require('ssh2').Client;
 
var conn = new Client();
conn.on('ready', function() {
  console.log('Client :: ready');
  conn.exec('apt updat', 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: 'localhost',
  port: 22,
  username: '<username>',
  password: '<password>'
});

Output is

STDERR: 
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.


STDERR: E: Invalid operation updat
STDERR: 

Stream :: close :: code: 100, signal: undefined

syedhabib53 avatar Sep 30 '20 08:09 syedhabib53

@syedhabib53 Can you manually apply the fix in https://github.com/steelbrain/node-ssh/pull/342 and see if your problem is fixed?

steelbrain avatar Oct 01 '20 15:10 steelbrain

@steelbrain no luck :-( Still getting the stderr in stdout

Result {
  code: 100,
  signal: null,
  stdout: '<password>\r\n' +
    '[sudo] password for <username>: \r\n' +
    '\u001b[1;31mE: \u001b[0mInvalid operation updat\u001b[0m',
  stderr: ''
}

syedhabib53 avatar Oct 06 '20 07:10 syedhabib53

Well the code in this package is doing the same thing mentioned in your snippet so not sure what's up 🤷‍♂️ You're more than welcome to debug and share your findings

steelbrain avatar Oct 06 '20 09:10 steelbrain