my problem is i try to make an button on a Website such as Submit to execute an bash script on server side my first try was with Php an Exec but it dont work html site:
<html>
<form action="exec.php" method ="get">
<input type="submit" value="Bestätigen">
</form>
</html>
php script:
<?php
shell_exec("/var/www/html/testsite/sc.sh");
header('Location: http://192.168.2.1/hs.html?success=true');
?>
shell script:
#!/bin/bash
touch /tmp/testfile
the www-data user owns all of these files if i click the button it only resend me to the http://192.168.2.1/hs.html?success=true but doesnt execute the script if i execute the php with ssh it works
please anyone check my code pls and help me if there is an better option to execute a shell script with websites it would make me happy to know them.
CodePudding user response:
Check first if this function is not disabled in php.ini file. Take a look on this documentation
If it is not, then check the value returned by shell_exec(). According to shell_exec() documentation, this function will return:
A string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.
Try code like this in the php script page. Please note that by doing this, Location header will fail.
<?php
$retVal=shell_exec("/var/www/html/testsite/sc.sh");
if (!$retVal) {
echo "<p>Pipe was not established</p>";
} else
echo "<p>Pipe was established:<br>";
if (is_null($retVal) {
echo "No value returned or command nod executed";
} else {
echo "Value returned: $retVal";
}
echo "</p>";
}
# header('Location: http://192.168.2.1/hs.html?success=true');
?>
If $retVal
is set to false then you'll see Pipe was not established message. Otherwise if it is not false but set to null means command was not executed or was executed and there was no output .
In this scenario, if you're expecting any output consider the following:
There are also some other things you'd like to try. Such as using touch complete path (/usr/bin/touch) instead of just using command name.
Regards
CodePudding user response:
Your php.ini is different in shell. Use phpinfo() trough web to check some settings which prevents the execution. Use the php.ini used by www-data (you can get it from phpinfo) and execute the script with user www-data and with that ini in shell.
php -c /path-to-custom/php.ini your-script.php
OR: simply turn on php error logging and check the logs.