NodeJS 响应 shell 提示
NodeJS respond to a shell prompt
我从 NodeJS 应用启动 shell 脚本。
在某个时刻,shell 脚本要求用户输入(Y/N 确认)。
节点应用程序在 RaspberryPi 上 运行 并且确认是通过物理按钮(无键盘)完成的,因此 "prompt answer" 必须通过代码完成。
我试过了:
childProcess.stdin.write("y\n");
childProcess.stdin.end();
但什么也没发生。
明确地说,我已经测试过在控制台上手动输入 "y" 并且它有效,脚本继续其过程
这是我的简单代码:
节点应用程序:
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;
shell 脚本:https://github.com/nfc-tools/miLazyCracker/blob/master/miLazyCracker.sh
这是简单的控制台输出:
[Useless start of the script]
Do you want clone the card? Place card on reader now and press Y [y/n]
sent prompt confirmation
问题出在 sh 脚本本身,它显然使用了无法从脚本获得答案的提示方法,我使用常规读取方法对其进行了修改,现在它可以工作了。
感谢您的评论
我从 NodeJS 应用启动 shell 脚本。
在某个时刻,shell 脚本要求用户输入(Y/N 确认)。
节点应用程序在 RaspberryPi 上 运行 并且确认是通过物理按钮(无键盘)完成的,因此 "prompt answer" 必须通过代码完成。
我试过了:
childProcess.stdin.write("y\n");
childProcess.stdin.end();
但什么也没发生。
明确地说,我已经测试过在控制台上手动输入 "y" 并且它有效,脚本继续其过程
这是我的简单代码:
节点应用程序:
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;
shell 脚本:https://github.com/nfc-tools/miLazyCracker/blob/master/miLazyCracker.sh
这是简单的控制台输出:
[Useless start of the script]
Do you want clone the card? Place card on reader now and press Y [y/n]
sent prompt confirmation
问题出在 sh 脚本本身,它显然使用了无法从脚本获得答案的提示方法,我使用常规读取方法对其进行了修改,现在它可以工作了。 感谢您的评论