can't executed bash from PHP..

dear list,
i've just write a simple php script to add user to my iptables, but some times it works and it dosn't work,, here's my script for my adduser.php

<?php

exec('/var/www/html/backup.sh');
$hash = "# $_POST[username]\n";
$ip = "-A INPUT -p tcp -m tcp -s $_POST[ipaddress] -d 10.10.105.18 --dport 8080 -j ACCEPT\n";
$myFile = "/etc/sysconfig/iptables.ok";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$_POST[username]\n";
$stringData = "$_POST[ipaddress]\n";
fwrite($fh, $hash);
fwrite($fh, $ip);
fclose($fh);
exec('/var/www/html/restore.sh');
 ?>

here's my bash script
backup.sh

#!/bin/bash
set -x
dir=/etc/sysconfig/
#file=/etc/sysconfig/iptables
#backup=/etc/sysconfig/iptables.bak
#ok=/etc/sysconfig/iptables.ok

cd $dir
    cp iptables iptables.bak
    sed -e :a -e '$d;N;2,21ba' -e 'P;D' iptables > iptables.ok
    chmod 646 iptables.ok
exit

and

restore.sh
#!/bin/bash
dir=/etc/sysconfig/
file=/etc/sysconfig/iptables
backup=/etc/sysconfig/iptables.bak
ok=/etc/sysconfig/iptables.ok

mv $ok $file
chmod 644 $file
cat /var/www/html/footer.txt >> $file
rm -rf $ok
exit

if i try my script form my bash shell, it's working well,, i do have gave apache permission to acces both my bash script. pls help me to find out the solution,,

rgds,
Ridwanfi

why don't you do the backup and restore shell scripts in PHP?

// bakup.sh
$bak = "iptables.ok";
$myfile = file("file");
$tail = 21;
$to_get = count($myfile) - $tail;
$mytailedfile = array_slice($myfile,0,$to_get);
$output=implode("",$mytailedfile);
file_put_contents($bak,$output);
chmod($bak,0646);

// restore.sh
rename($bak $myfile);
chmod($myfile,0644);
$string_to_append = file_get_contents("/var/www/html/footer.txt");
file_put_contents($myfile,$string_to_append,FILE_APPEND | LOCK_EX));
unlink($bak);

dear ghostdog74
thank's for your reply and great solution, i'm still newbi and sure will read and learn more often. :):):):slight_smile: