what is the function in php that used to execute files in linux. i want to execute script file that contains iptables rules
Printable View
what is the function in php that used to execute files in linux. i want to execute script file that contains iptables rules
how i can use exec function in php to execute iptables command
Wow...just wow...
Exec Function - PHP Manual
Copy/paste from the PHP function manual:
Code:<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>
With iptables, things are a bit more complicated than the command whoami due to the fact that only root can execute iptables command. But we can get around this with sudoers. You need to add the web server user (for example, apache) to /etc/sudoers like this
apache ALL=NOPASSWD:/sbin/iptables
then in your php script you put your command something like
$cmd="sudo /sbin/iptables Blah Blah Blah";
exec($cmd);
if you don't see /etc/sudoers, you need to get it installed first.
i add this line to sudoers
apache ALL=NOPASSWD:/sbin/iptables
then i write php script like this
<?php
$cmd="sudo /sbin/iptables -A INPUT -p tcp -s 172.16.58.10 -j REJECT";
exec($cmd);
?>
but i didn't get the result.
thanks for help.
shyma,
You need to find out the what the www user is first. If you are using apache, look under /etc/apache/uid.conf. Replace "apache" in "apache ALL=NOPASSWD:/sbin/iptables" with the User setting in uid.conf and try again. Also if you can post the error message returned by php if you need further help.