NodeJS 从节点js中的stdout阅读

qfe3c7zg  于 2022-12-12  发布在  Node.js
关注(0)|答案(1)|浏览(133)

我在系统上执行了一个命令,并希望看到没有stdout属性的命令结果。

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)

`
我试着在谷歌上搜索,但没有找到任何东西,这是结果。

<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

我只要最后一点

uurity8g

uurity8g1#

您的代码对我来说工作正常。只需注解掉第8行中的console.log(stdout)和第21行中的console.log(commandOutPut),就可以开始了.. screenshot

相关问题