A
A
Alexander Fadeev2016-06-22 13:28:35
Node.js
Alexander Fadeev, 2016-06-22 13:28:35

How to redirect a stream and write data to a variable?

Good afternoon, I'm trying to understand node, the task arose to get data from the server via ssh using the "ssh-exec" module.
Example code:

var exec = require('ssh-exec');
 
exec('who', {
  user: 'user',
  host: '192.168.1.1',
  
  password: 'password'
}).pipe(process.stdout)

returns the result of the "who" command to the console, but I can't figure out how to save the result to a variable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
de1m, 2016-06-22
@de1m

I think that you need to use the second example, where there is a callback. Something like this:

var exec = require('ssh-exec')  

exec('ls -lh', {
    "user": "de1m",
    "host": "localhost",
    "password": "pass"
}, function (err, stdout, stderr) {
  console.log(err, stdout, stderr)
})

err - if there is an error in the connection
stdout - if everything is fine
stderr - if the command returns something more than "0"
But in general I would recommend using the "ssh2" module directly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question