shell 脚本在 HTML/PHP 中执行

shell script executed in HTML/PHP

我对 HTML/PHP 有一些基础知识。我面临的情况令人沮丧。我想要完成的是在网页上创建一个简单的搜索框,当用户输入并单击提交时,我的 shell 脚本将被执行,然后显示在 php 页面上。当我单击提交以确保 PHP exec shell 命令正常工作时,我已经成功地将其他命令发送到 运行。 我将在网页上看到输出。只是不是我的剧本。我的脚本使用一个参数通过命令行传递和工作。下面是我的脚本、HTML 和 PHP 页面的详细信息。另外,我使用的是 FreeBSD 10 盒子。

我的脚本

命令行 - $ csearch "argument"

#!/bin/sh
grep -ir -B 1 -A 4 "$*" /usr/local/var/rancid/CiscoDevices/configs

我的 HTML 页面

<html>
<body>
<form method="POST" action="csearch.php">
    <input type="text" name="searchText">
    <input type="submit" value="Search">
</form>
</body>
</html>

我的 PHP 页面

<?php
$searchText=$_POST['$searchText'];
?>

<html>
<?php

$output = shell_exec('/usr/local/bin/csearch $searchText');
echo "<pre>$output</pre>";

?>
</html>

非常感谢任何帮助。

shell_exec('/usr/local/bin/csearch $searchText');这不是你所期望的:

<?php

$searchText = 'foobar';
$cmd = '/usr/local/bin/csearch $searchText';
echo $cmd;

?>

输出:

/usr/local/bin/csearch $searchText

更改字符串以使用双引号,$searchText 实际上就是您想要的:

$output = shell_exec("/usr/local/bin/csearch $searchText");

More info on the use of quotes in PHP.

正如@uri2x 在评论中暗示的那样:

出于类似的原因,

$searchText=$_POST['$searchText']; 应更改为 $searchText=$_POST['searchText'];