I want to execute bash command with script. If I echo the command, I get the proper response. But if I execute it from browser, it does not work. If I echo the command ls
, it is executed and shown. I have granted all permissions. If I write command in terminal it works.
<?php
$banlista = $_POST['banlista'];
$ip = $_POST['ip'];
$command = "fail2ban-client set $banlista banip $ip";
$sporocilo = shell_exec("$command");
?>
It's better to run the command within the php code. You can get a response from the actual command and verify that it worked.
If you have your code this way
<?php
$banlista = $_POST['banlista'];
$ip = $_POST['ip'];
$command = "fail2ban-client set $banlista banip $ip";
$sporocilo = shell_exec("$command");
?>
You can try to add this:
<?php
exec("sudo user /usr/bin/fail2ban-client set $banlista banip $ip", $output, $return);
echo "Failtoban client returned $return, and output:
";
var_dump($output);
?>
You're probably missing a sudo and a user who has the right to run the command from the browser.