我需要在 Node.js 中编写这个
vCmd = any cmd that needs root privileges
vPwd = "1234" #asked before in a seperate dialog
vProc = subprocess.Popen(["sudo", "-S", "xfce4-terminal", "-e", vCmd], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
vResult = vProc.communicate(vPwd + '\n')[1]
(也许可以使用嵌套的child_process.spawn)
重要的是以编程方式使用 sudo 和密码
最佳答案
使用node的child_process
模块中的spawn
函数,并将sudo密码写入stdin:
var spawn = require('child_process').spawn;
var vCmd = '<command to execute>';
var args = [ '-S', 'xfce4-terminal', '-e', vCmd ];
// The sudo password
var pw = '******';
// Spawn the sudo command
var proc = spawn( 'sudo', args );
// Log stdout to console.
proc.stdout.on('data', ( data ) => {
console.log( data.toString() );
});
// Send password to stdin.
proc.stdin.write( pw+'\n' );
// Close stdin.
proc.stdin.end();
关于python - 如何在node.js中实现这个python代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44607462/