doufang1954 2015-09-05 15:38
浏览 75
已采纳

用HTML / PHP执行的shell脚本

I have some basic knowledge of HTML/PHP. The situation I am facing is frustrating. What I want to accomplish is to create a simple search box on a web page, when the user puts in input and clicks submit then my shell script is executed and then presented on a php page. I have been successful in getting other commands to run when I click submit to make sure the PHP exec shell command is working. I will see the output on the web page. Just not my script. My script uses an argument to pass and works thru command line. Below is the details of my script, HTML, and PHP page. Also, I'm using a FreeBSD 10 box.

My Script

Command Line - $ csearch "argument"

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

My HTML page

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

My PHP page

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

<html>
<?php

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

?>
</html>

Any help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dqm83011 2015-09-05 15:51
    关注

    shell_exec('/usr/local/bin/csearch $searchText'); This isn't what you expect it to be:

    <?php
    
    $searchText = 'foobar';
    $cmd = '/usr/local/bin/csearch $searchText';
    echo $cmd;
    
    ?>
    

    outputs:

    /usr/local/bin/csearch $searchText

    Change the string to use double quotes and $searchText will actually be what you want to it be:

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

    More info on the use of quotes in PHP.

    As @uri2x hints in a comment:

    $searchText=$_POST['$searchText']; should be changed to $searchText=$_POST['searchText']; for a similar reason.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集