Search code examples
node.jsraspberry-pipromptchild-process

NodeJS respond to a shell prompt


I launch a shell script from a NodeJS app.
At a certain point, the shell script ask for a user input ( a Y/N confirmation ).
The node app is running on a RaspberryPi and the confirmation is done by a physical button (no keyboard) so the "prompt answer" has to be done by code.
I tried :

childProcess.stdin.write("y\n");
childProcess.stdin.end();

but nothing happens.
Just to be clear, I've tested entering the "y" manually on the console and it works, the script continues its course

Here's my simplyfied code:

node app:

var Gpio = require('pigpio').Gpio;
class Main {
  constructor() {
    this.prompt = false;
    const button = new Gpio(17, {
      mode: Gpio.INPUT,
      pullUpDown: Gpio.PUD_DOWN,
      edge: Gpio.EITHER_EDGE
    });
    button.on('interrupt', (level) => {
      if (level == 1) {
        this.onButtonPress();
      } else if (level == 0) {
        this.onButtonRelease();
      }
    });
    this.childProcess = require('child_process').spawn('sudo', ['miLazyCracker']);
    this.childProcess.stdout.on('data', (dataBuffer) => {
      var data = dataBuffer.toString();
      console.log(data);
      if (data.includes("Do you want clone the card?")) {
        this.prompt = true;
      }
    });
  }
  onButtonPress() {

  }
  onButtonRelease() {
    if (this.prompt) {
      this.childProcess.stdin.write("y\n");
      this.childProcess.stdin.end();
      console.log("sent prompt confirmation");
    }
  }
}
new Main();
module.exports = Main;

The shell script : https://github.com/nfc-tools/miLazyCracker/blob/master/miLazyCracker.sh

Here is the simplyfied console output:

[Useless start of the script]

Do you want clone the card? Place card on reader now and press Y [y/n]
sent prompt confirmation

Solution

  • The problem was into the sh script itself which apparently uses a prompt method that was not being able to get answers from a script, I modified it using the regular read method and now it works.. Thanks for your comments