node-ssh
node-ssh copied to clipboard
stderr always coming in stdout
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 Can you manually apply the fix in https://github.com/steelbrain/node-ssh/pull/342 and see if your problem is fixed?
@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: ''
}
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