can't executed bash from PHP..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting can't executed bash from PHP..
# 1  
Old 04-16-2009
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

Code:
<?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
Code:
#!/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
Code:
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
# 2  
Old 04-16-2009
why don't you do the backup and restore shell scripts in PHP?
Code:
// 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);

# 3  
Old 04-29-2009
dear ghostdog74
thank's for your reply and great solution, i'm still newbi and sure will read and learn more often. SmilieSmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux/bash Script only working if executed from shell prompt

Hi, maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt? #! /bin/bash set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin date >>... (3 Replies)
Discussion started by: beislhur
3 Replies

2. UNIX for Dummies Questions & Answers

Bash script dont works when executed as cronjob

Hello, i have cronjob: crontab -l * * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh it runs: /var/log/cron Mar 25 12:11:01 vps crond: (root) CMD (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh) but somehow script dont run properly via cronjob. But when i execute cronjob... (7 Replies)
Discussion started by: postcd
7 Replies

3. Shell Programming and Scripting

Technical questions on bash,mysql and pHp

1. bash -bash escape rules, esp. ',", -how to use Ctrl+R reverse cmd search with regex? 2. mysql -how to use grep in mysql 3. php -why !0 is not evaluated to true? what's its value -php getopt: what if there is duplicate in cmdline args (2 Replies)
Discussion started by: John_Peter
2 Replies

4. Shell Programming and Scripting

help with shell script executed by php.

I made a shell script to execute a server in screen mode. # start server screen -d -m -S Test ./application echo "Program Started Successfully" than I'm executing it from php by echo shell_exec('/home/script.sh'); and it is giving me this error. "cannot make directory... (1 Reply)
Discussion started by: dmallia
1 Replies

5. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

6. Shell Programming and Scripting

bash script + php

Hello, I want to move some of my bash scripts to php ( add samba user, add new PC in dhcp and etc. ).With Google, I found some articles for bash and php, but there is only simple examples, nothing for variables. Example: #!/bin/bash status=`/usr/local/etc/rc.d/isc-dhcpd status` ############... (1 Reply)
Discussion started by: mrowcp
1 Replies

7. Red Hat

php file is not executed

hello i have installed php on my FC 10 Machine and made the necessary changes. like:- AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps in the "httpd.conf" But when i run php file it opens file as "Save As" dialogue box. anyone can guess why ?... (5 Replies)
Discussion started by: phpalerts
5 Replies

8. Shell Programming and Scripting

rsync bash php

Hello, I have a bash script update.sh that contains rsync --delete -avz -e ssh ${files} root@64.XX.XX.XX:/websites/red/ when I use that script ./update.sh as root, it works like a charm as I set up the private/public key properly. When run from a php script through the apache webserver <?php... (10 Replies)
Discussion started by: JCR
10 Replies

9. Shell Programming and Scripting

how to make a bash script that can be executed by people simultaneously?

dear friends, i want to make a bash script that can be executed by many people simultaneously. do you have any idea to make it? there will be many dependent-variables(which is input from people) in the scripts. i am thinking about a random temporary file that created by the bash script each... (4 Replies)
Discussion started by: jimmbp
4 Replies

10. Shell Programming and Scripting

bash scripting cannot executed in crontab

hi guys, i have a problem. a week ago i made a successful crontab that execute bash scripting daily, it worked well but now, it doesn't work at all, in the mail i have: " /home/jimmy/cha/scripts/cekpderr produced the following output: lagi jalan /home/jimmy/cha/scripts/cekpderr:... (6 Replies)
Discussion started by: jimmbp
6 Replies
Login or Register to Ask a Question