Search code examples
node.jsstdinchild-process

Node.js input password to a bin file being run via spawn


I am running a .bin file via child_process.spawn(), which takes in some inputs from the user before completing the setup. It seems to work fine taking all the inputs correctly via process.stdin.write("input"\n);. However, the same doesn't work when the password is sent via stdin. Directly running the bin file and manually entering the password works. Is there some format I am supposed to set before sending the password via node.js? I just keep seeing * being logged on stdout continuously and the setup doesn't seem to proceed further. Below is the snippet that I am using

var child_process = require('child_process');
var process = child_process.spawn('./test.bin');

process.stdout.on('data', function(data) {
if(data.toString().trim() === 'Username:')
    process.stdin.write("test\n"); // This works
else if (data.toString().trim() === 'Password:')
    process.stdin.write("password\n"); //This doesn't

Any inputs on the same might be helpful.Thanks.

Please note that when the password is entered by directly running the bin file, upon typing the password, nothing is displayed, but entering the correct password works. So, I am thinking there might be some encoding issues or something like that which I may be missing.


Solution

  • Answering my own question, it seemed to work by entering the password string one character at a time like below:

    process.stdin.write(password[0]);
    process.stdin.write(password[1]);
    process.stdin.write(password[2]);
    process.stdin.write(password[3]);
    process.stdin.write("\n");