PHP problem with shell_exec() command

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shenmu0
Forum Newbie
Posts: 4
Joined: Mon Apr 09, 2007 4:08 pm

PHP problem with shell_exec() command

Post by shenmu0 »

Hello all,
I've came up with an issue when trying to work on Linux's shell using PHP
it seems to work when I issue the following commands, shell_exec("iptables -h");, shell_exec("ls"); and shell_exec("ifconfig");
however when I'm trying to do something like this, shell_exec("iptables -L") or shell_exec("iptables restart");
it doesnt seem to work
I am a PHP and Linux novice.... :(
Could somebody kindly help me, I am desparate to get this working :(
Thankyou
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

Have you checked to see if the commands work directly on the command line on the machine you're doing this on? Personally neither 'iptables restart' nor 'iptables -L' work on my laptop here. Do you get any output from the failing shell_exec commands (shell_exec should return the entire output of the command, ie stdout and stderr).
shenmu0
Forum Newbie
Posts: 4
Joined: Mon Apr 09, 2007 4:08 pm

Post by shenmu0 »

Thanks for your reply,
the commands such as "iptable -L" works fine in the original shell
but there are just no output nor response when I do it through shell_exec().
However for some reason, the shell_exec could execute "ifconfig" and deliver the output on the browser.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I bet the user account under which apache is running doesn't have the permission to manipulate system firewall.
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

Have you tried running it as the webserver user?
shenmu0
Forum Newbie
Posts: 4
Joined: Mon Apr 09, 2007 4:08 pm

Post by shenmu0 »

I know this isnt a very good idea, but I did all this in root
Would that be the reason it didnt work?
shenmu0
Forum Newbie
Posts: 4
Joined: Mon Apr 09, 2007 4:08 pm

Post by shenmu0 »

I've tried several commands, such as "ls -l","ifconfig" and "iptables -h", these all works and return the result on the browser.
However when I'm trying to do "iptables -L" which shows the currently configuration in my firewall, nothing happens..

this is my code: $output = shell_exec ("iptables -L");
echo $output;

I also tried the "service iptables restart" command using shell_exec, however it returned: "Unloading iptables module [FAILED], Applying iptables firewall [FAILED].

This means that there are results for certain commands, is it a problem with permissions?
I heard some people use "sudo" to authorize the access, but I don't know how to use it.

Please give me some advice
Post Reply