Respond to password query in child_process spawn nodejs
Asked Answered
P

0

7

Need help figuring this one out.

I am trying to call an external application through child process spawn and after calling the application it requests a password. I then get a fatal error and the child process exits. I have tried a few different combinations on where to place the child.stdin.write('somepassword\n') but i get the same error. I think its related to the child not being seen by the application as a terminal.

Sample Code:

const { spawn } = require('child_process');

const child = spawn('./some-app',[arguments],{shell:true});

// process.stdin.pipe(child.stdin)

child.stdin.write('testpw\n'); // has no effect

child.stdout.on('data', (data) => {
    console.log(`child stdout:\n${data}`);
    //child.stdin.write('testpw\n'); // no effect here either
});

child.stdin.end(); 

child.stderr.on('data', (data) => {
    console.error(`child stderr:\n${data}`);
});

child.on('exit', function (code, signal) {
    console.log('child process exited with ' +
                `code ${code} and signal ${signal}`);
});

Terminal Output

> node spawn.js

child stdout:
Enter password: Fatal error:
  Unix.Unix_error(Unix.ENOTTY, "tcgetattr", "")

child process exited with code 1 and signal null

The error leads me to believe that the environment that the child_process sees is not a tty but searching on how to make the child see it that way is sending me down a deep unix rabbit hole and doesn't stand out with node related answers. I am running this on Ubuntu 16.04 and with nodejs V10.15

Edit If i change the stdio streams for the child.stdin the process no longer has the fatal error, but now just sits and waits for keyboard input from a user. But writes to process.stdin.write() will display on the screen, but the child wont respond unless i physically type in on my keyboard.

const { spawn } = require('child_process');

const child = spawn('./some-app',
                    [arguments],
                    {
                     shell:true,
                     stdio:['inherit','pipe','pipe']
                    });


child.stdout.on('data', (data) => {
    console.log(`child stdout:\n${data}`);
    process.stdin.write('testpw\n'); // displays but not handled by the 
                                     // process
}); 

child.stderr.on('data', (data) => {
    console.error(`child stderr:\n${data}`);
});

child.on('exit', function (code, signal) {
    console.log('child process exited with ' +
                `code ${code} and signal ${signal}`);
});

Terminal Output

> node spawn.js

child stdout:
Enter password:
testpw         <-- process sits here waiting for real keyboard input
Pythagoras answered 11/1, 2019 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.