gpt4 book ai didi

javascript - 从网页执行 Linux 命令

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:41 25 4
gpt4 key购买 nike

我已经在 apache2 服务器(在 Ubuntu 18.04 上运行)上建立了一个非常基本的网站。该网站只有一页。我使用一些 JavaScript 向其添加动态内容,该网站包含 HTML 表单,该表单将数据发送到 MySql 服务器——我使用 PHP 连接到数据库。

根据用户输入,我需要执行某个 Linux 终端命令(例如:ls -l)。例如,如果用户输入名字和姓氏并单击提交按钮,则 ls -l 命令将自动执行,而无需用户在终端中执行任何操作。这可能吗?这是我的 HTML 表单:

<form method="post">
<label>Fisrt Name: <input type="text" name="firstname" required></label>
<label>Last Name: <input type="text" name="lastname" required></label>
<input type="submit" name="submit" value="Submit">
</form>

编辑:好的,除了连接到 MySql 数据库的代码之外,我现在还有这个 PHP 代码:

<?php
$output = shell_exec('sudo addgroup test2');
echo "<pre>$output</pre>;

$result = exec('echo $?');
echo "$result";
?>

但是,它不会添加该组并且 echo $? 返回 0。但是,当我用 替换 shell_exec('sudo addgroup test2'); 时shell_exec('ls -l'); - 它工作正常。我做错了什么?

最佳答案

这需要由在 Apache 后面运行的后端服务器代码处理。您可以将命令作为查询字符串或作为来自 UI 的 POST json 传递,并编写服务器端脚本(在 php 中)以捕获参数并作为 shell-exec http://php.net/manual/en/function.shell-exec.php 执行函数。 .

关于javascript - 从网页执行 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com