Search code examples
javascriptnode.jscommand-promptchild-process

How can I respond to a prompt in Node.JS child process


I'm trying to write a program that runs a series of commands as my other user account, which is an administrator, however I'm running into an issue where the "Please enter the password" part of this is completely skipped, filled in with random blank spaces for no reason.

How do I go about responding to prompts in Node.JS child procsses?

Here's my code:

var {spawn} = require("child_process");
var cmd = spawn("cmd.exe");

var sleep = t => new Promise(r => setTimeout(r, t * 1000));

cmd.on('close', code => interact({message: "Command executed."}));
// "interact" is a pop-up function, it'll be removed when testing is done, it functions as expected no issues.
cmd.stdout.on('data', text => console.log(Buffer.from(text).toString()));
cmd.stderr.on('data', text => console.log('Err:', Buffer.from(text).toString()));

cmd.stdin.write("runas /profile /user:Manxy \"cmd.exe\"\n");
await sleep(.2);
cmd.stdin.write("ThisIsThePassword\n");
await sleep(.2);
cmd.stdin.write("exit\n");

Here's the response: Image of the response


Solution

  • RUNAS directly prevents people from using it insecurely, however I can include this file with my package and use it instead: https://learn.microsoft.com/en-us/sysinternals/downloads/psexec

    Example; psexec.exe -u Manxy -p "ThisIsThePassword" cmd.exe

    This greatly simplifies it for me.

    Thank you, jsejcksn for the link