如何在node.js中实现这个python代码片段

时间:2017-06-17 17:32:30

标签: python node.js linux

我需要在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和密码

1 个答案:

答案 0 :(得分:0)

在节点的child_process模块中使用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(); 函数,并将sudo密码写入stdin:

{{1}}