Home > database >  reading from stdout in node js
reading from stdout in node js

Time:12-08

I executed a command on the system and want to see the results of the command with out the stdout properties coming in my way `

const prompt = require('prompt-sync')({ sigint: true });
const { exec } = require('child_process');
const { stdout } = require('process');
const pathToElevate = Number(prompt('1- suid -2 binary root 3- cap: '))
const userPassword = prompt('Please put the user password(optinal): ')


console.log(stdout)

if (pathToElevate === 1) {
    var commandOutput = exec('ls', (error, stdout, stderr) => {
        if (error) {
            console.log(`error ${error.message}`)
            return;
        } if (stderr) {
            console.log(`stderr ${stderr.message}`)
            return;
        }
        return console.log(stdout) // here is my problem, I want to parse stdout to show the results of ls
    })
    console.log(commandOutput)

`

I tried searching on google but didn't find any thing and here is the result `

<ref *1> ChildProcess {
  _events: [Object: null prototype] {
    close: [Function: exithandler],
    error: [Function: errorhandler]
  },
  _eventsCount: 2,
  _maxListeners: undefined,
  _closesNeeded: 3,
  _closesGot: 0,
  connected: false,
  signalCode: null,
  exitCode: null,
  killed: false,
  spawnfile: '/bin/sh',
  _handle: Process {
    onexit: [Function (anonymous)],
    pid: 8058,
    [Symbol(owner_symbol)]: [Circular *1]
  },
  spawnargs: [ '/bin/sh', '-c', 'ls' ],
  pid: 8058,...

a.out
index.js
node_modules
package.json
package-lock.json

I only want the last bit

CodePudding user response:

Your code works OK for me. just comment out the console.log(stdout) in line 8 and console.log(commandOutPut) in line 21 and you are good to go.. screenshot

  • Related