使用PHP问题执行Bash脚本

时间:2013-06-13 19:09:59

标签: php bash echo execute

我有一个bash脚本,它绑定在一些我想在网页中执行的python脚本中,并将它创建的文件捕获到页面上。目前我根本无法执行脚本(多次测试脚本以确保它实际上正常工作),而且我似乎无法找到我的错误所在。第一组代码来自我的'dashboard.php'文件,该文件位于/ var / www中。我已将脚本的权限更改为777并使其成为可执行文件。

<html>
<body>

<form action="welcome.php" method="post">
    IP/CIDR Input: <input type="text" name="cidriptext" />
    Single IP: <input type="checkbox" name="singlech" value="single" /> 
    CIDR Range: <input type="checkbox" name="cidrch" value="cidr" />    
<input type="submit" value="Submit">
</form>

<form action="welcome.php" method="post">
    List of IP's: <input type="text" name="listname" />
<input type="submit" value="Submit">
</form>

</body>
</html>

第二个脚本是页面('welcome.php'),它通过表单输入来执行脚本。此时我不希望$listname_text正常工作,但我确实希望在没有选中框时出现'错误输入'错误,最后是'测试'输出,以及正在创建的文件在后端。

<?php
    $ipcidr_text = $_POST['cidriptext'];
    $listname_text = $_POST['listname'];

    if !(empty($listname_text):
        shell_exec('/var/www/getStarted -l $ipcidr_text');
        $output0 = shell_exec('echo "List of IP's"');
        echo "<pre>$output0</pre>";

    elseif (isset($_POST['cidrch'])):
        shell_exec('/var/www/getStarted -c $ipcidr_text');
        $output1 = shell_exec('echo "CIDR Range"');
        echo "<pre>$output1</pre>";

    elseif (isset($_POST['singlech'])):
        shell_exec('/var/www/getStarted -s $ipcidr_text');
        $output2 = shell_exec('echo "Single IP"');
        echo "<pre>$output2</pre>";

    else:
        $output = shell_exec('echo "Incorrect input"');
        echo "<pre>$output</pre>"; 

    endif;

    $testing = shell_exec('echo "testing"');
    echo "<pre>$testing</pre>";

?>

PHP正在运行,我能够执行基本脚本:

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

如果您需要更多信息,请告诉我们。感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我过去曾遇到shell_exec()的问题,而倾向于依靠system(),而是尝试使用"代替'来封装命令。您可能还需要更改调用脚本的方式:

自:

/var/www/getStarted

To(专门调用python解释器):

/usr/bin/python /var/www/getStarted