NodeJS执行' su'命令和密码提示

时间:2016-03-31 10:41:21

标签: javascript node.js command-line-interface

使用命令行应用程序。我需要允许用户通过su

切换到root

我已经尝试了

var spawn = require( 'child_process' ).spawn,
        ls = spawn('su');

    ls.stdout.on( 'data', ( data ) => {
        console.log( "stdout: ${data}" );
    });

    ls.stderr.on( 'data', ( data ) => {
        console.log( "stderr: ${data}" );
    });

    ls.on( 'close', ( code ) => {
        console.log( "child process exited with code ${code}" );
    });

并同步:

var fs = require('fs'),
    spawnSync = require('child_process').spawnSync;

spawnSync("su");

if(fs.existsSync('./command_stdout.txt')){
  // process standard output from your command here
}
if(fs.existsSync('./command_stderr.txt')){
  // process error output from your command here
}

但这些都没有问我提示。

0 个答案:

没有答案