[英]How to execute sh script or shell commands from browser input using a node server?
我正在尝试从 Web 接口重新启动或关闭 Raspberry Pi。 我过去使用 PHP 的shell_exec
管理过这个问题。
这次我尝试专门使用JavaScript
这样做。
对于前端,我选择VueJS
。 对于后端,我不知道该选择什么; 我见过NodeJS
、 Express JS
和大量能够在 UNIX OS 设备(例如 Raspberry Pi)上运行的服务器。
我已经设法使用child_process
并使用node./test.js
从测试文件中运行 shell 命令,但是我该怎么做呢?
如何将Restart Button
从 Vue 链接到节点或 Express 服务器?
这是我用来测试的 Vue 组件。 它包含一个输入,而不是一个按钮,只是为了测试简单的 shell 命令。
<template>
<div id="wrapper">
<div class="input_row">
<input type="text" class="input_text" id="shell" v-model="shell_command" />
<label class="label_" for="shell">Type Shell Command</label>
</div>
<button type="button" class="button" @click="shellExec">Submit !</button>
</div>
</template>
<script>
import { exec } from "child_process";
export default {
name: "ShellExec",
components: {},
data() {
return {
shell_command: ""
};
},
methods: {
async shell() {
return new Promise((resolve, reject) => {
exec(this.shell_command, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
});
},
async shellExec() {
let { stdout } = await this.shell();
for (let line of stdout.split("\n")) {
console.log(`ls: ${line}`);
}
}
}
};
</script>
<style scoped>
</style>
我还没有决定使用服务器,但我看过一些 Express 服务器的例子。
child_process
是一个 node.js 库,在浏览器中不可用。 您可以将您的应用程序转换为 electron 应用程序,您可以在其中将其配置为可用(并且仅在您正在执行的主机上使用),或者设置您的应用程序可用于触发命令的 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.