How do I use node.js child_process to programatically allow me to supply a password.
如何使用node。js child_process程序允许我提供一个密码。
const spawn = require('child_process').spawn;
const ls = spawn('sudo ls');
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}`);
});
The above throws an error. I am trying to figure out where exactly I can catch where it is wanting the password input and supply that via an environment variable.
上面的操作会抛出一个错误。我正试图弄清楚在哪里可以确切地捕获需要密码输入的位置,并通过环境变量提供密码输入。
I am hoping I can catch when it says Password
check to make sure its saying that and then pass the password.
我希望我能抓住它说的密码检查,确保它说了,然后传递密码。
1 个解决方案
#1
3
The error is because you're using the spawn command incorrectly.. It should be:
错误在于您不正确地使用了spawn命令。应该是:
const ls = spawn('sudo', ['ls']);
Arguments to the child process are specified as the second argument (in an array).
子进程的参数被指定为第二个参数(在数组中)。
You should then see the prompt for password, and I imagine be able to write to stdin to enter the password (which you're wanting to pass from process.env based on what you've said).
然后您应该会看到密码提示符,我想您可以向stdin写入密码(您希望从进程中传递密码)。基于你说的)。
Edit: Quick search on SO seems to suggest it may not be as straight forward and writing to stdin after the prompt..
编辑:在SO上快速搜索似乎表明它可能不那么直接,并在提示符之后给stdin写信。
#1
3
The error is because you're using the spawn command incorrectly.. It should be:
错误在于您不正确地使用了spawn命令。应该是:
const ls = spawn('sudo', ['ls']);
Arguments to the child process are specified as the second argument (in an array).
子进程的参数被指定为第二个参数(在数组中)。
You should then see the prompt for password, and I imagine be able to write to stdin to enter the password (which you're wanting to pass from process.env based on what you've said).
然后您应该会看到密码提示符,我想您可以向stdin写入密码(您希望从进程中传递密码)。基于你说的)。
Edit: Quick search on SO seems to suggest it may not be as straight forward and writing to stdin after the prompt..
编辑:在SO上快速搜索似乎表明它可能不那么直接,并在提示符之后给stdin写信。