Tom
Tom

Reputation: 11

Executing BashScript with PHP Issues

I have a bash script that's tied into some python scripts that I would like to execute within a webpage and cat the file it creates on to the page. Currently I haven't been able to execute the script at all (tested the script multiple times to ensure it is in fact working), and I can't seem to find where my error is. The first set of code is from my 'dashboard.php' file which is in /var/www. I have changed the permissions for the scripts to 777 and made executable.

<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>

This second script is the page ('welcome.php') it reaches out to execute the script with the input from the form. At this point I don't expect the $listname_text to work properly, but I do expect to get the 'Incorrect Input' error when no boxes are checked, the 'testing' output at the end, and the files being created on the backend.

<?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 is working, I am able to execute a basic script:

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

If you need anymore information, please let me know. Appreciate any help!

Upvotes: 0

Views: 256

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 13

I've had issues with shell_exec() in the past and tend to lean on system() instead, also try " instead of ' to encapsulate the command. You may also need to change how you call your script:

From:

/var/www/getStarted

To (call the python interpreter specifically):

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

Upvotes: 1

Related Questions