LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2011, 11:23 PM   #1
noony123
Member
 
Registered: Oct 2010
Posts: 167

Rep: Reputation: 0
Bash command from PHP !


Hi all.

Distro: Centos 5, PHP 5
I have done a bit research on running bash commands from php and there was little success.

This is my relevant php script

<?php

echo exec('pwd');

?>

Now this works fine and if i replace pwd with whoami, it also works fine. But this doesnt

echo exec('ping -c 4 192.168.1.1');

I have tried putting it like this also

echo exec('ping -c 4 192.168.1.1', $output);

then using foreach loop, i tried to print the result of output but no use. I tried shell_exec() and system() also but no use. Can someone pls guide me how to run any bash command from php ?
 
Old 07-14-2011, 11:38 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You'll prob need to specify the complete path to the ping cmd.
 
Old 07-14-2011, 11:41 PM   #3
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Dear Sir,

I did that as well, but still the page is showing nothing.

Is it possible that php is not installed properly ? but as i mentioned, some exec commands are working but others like 'ls' are not
 
Old 07-14-2011, 11:47 PM   #4
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by noony123 View Post
But this doesnt

echo exec('ping -c 4 192.168.1.1');

I have tried putting it like this also

echo exec('ping -c 4 192.168.1.1', $output);

then using foreach loop, i tried to print the result of output but no use. I tried shell_exec() and system() also but no use. Can someone pls guide me how to run any bash command from php ?
The exec() function returns the last line of the command. If you want to capture the complete output, use an array as the second parameter:

PHP Code:
exec("ping -c 4 localhost",$output);

foreach(
$output as $line) {
    echo 
"$line\n";

The code above outputs this:

Code:
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.043 ms
64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.039 ms
64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.042 ms
64 bytes from localhost (127.0.0.1): icmp_req=4 ttl=64 time=0.040 ms

--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.039/0.041/0.043/0.001 ms
Use var_dump() to view the array contents:

PHP Code:
exec("ping -c 4 localhost",$output);
var_dump($output); 
Outputs:

Code:
array(9) {
  [0]=>
  string(48) "PING localhost (127.0.0.1) 56(84) bytes of data."
  [1]=>
  string(68) "64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.043 ms"
  [2]=>
  string(68) "64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.039 ms"
  [3]=>
  string(68) "64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.042 ms"
  [4]=>
  string(68) "64 bytes from localhost (127.0.0.1): icmp_req=4 ttl=64 time=0.040 ms"
  [5]=>
  string(0) ""
  [6]=>
  string(33) "--- localhost ping statistics ---"
  [7]=>
  string(62) "4 packets transmitted, 4 received, 0% packet loss, time 2999ms"
  [8]=>
  string(49) "rtt min/avg/max/mdev = 0.039/0.041/0.043/0.001 ms"
}
For shell builtin commands, it's better to use shell_exec(). Remember to escape the strings used for these functions using escapeshellarg() and escapeshellcmd().

Last edited by Diantre; 07-14-2011 at 11:50 PM.
 
1 members found this post helpful.
Old 07-14-2011, 11:50 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try writing a short script eg
Code:
#!/bin/bash
set -xv
echo $PATH
ping -c 4 192.168.1.1  # use absolute path whatever it is on your system
and call that from exec and redirect output stdout & stderr to a log.
Also, check the ownership & permissions on ping


EDIT: Dang too slow; above poster knows more about this than me; not a php person...

Last edited by chrism01; 07-14-2011 at 11:51 PM. Reason: too slow ...
 
Old 07-15-2011, 01:32 AM   #6
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Sir I tried as you said, following is my php page

[root@WAN-Admin html]# cat first.php
<html>
<head> <title> PHP </title></head>

<body>

<?php

exec("ping -c 4 localhost", $output);

foreach($output as $line) {
echo "$line";

}


?>

</body>


</html>

Now when i open the page, and refresh, its blank !!

Can you pls guide me what i am missing. I also tried giving full path for ping command, it doesnt work as well.
 
Old 07-15-2011, 02:03 AM   #7
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
The only command that is running is "whoami"

If i try any other command, it simply doesnt work. The page shown is blank.

---------- Post added 07-15-11 at 03:03 AM ----------

Do i need to assign apache user any special privi to execute any command ? if thats the case then why whoami is working ?

Kindly guide me, really stuck here
 
Old 07-15-2011, 02:18 AM   #8
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by noony123 View Post
Now when i open the page, and refresh, its blank !!
Your code is ok, works for me. Just a couple of suggestions:

PHP Code:
<?php
exec
("ping -c 4 localhost"$output);

echo 
"<pre>";
foreach(
$output as $line) {
    echo 
"$line\n";
}
echo 
"</pre>";
?>
I added <pre> tags to keep the formatting of the output and a newline character (\n) inside the foreach so the output is separated into lines.

As for the blank page, it could be related to apache's configuration. Usually, apache loads PHP from a line in httpd.conf like this:

Code:
Include /etc/httpd/mod_php.conf
Make sure that your httpd.conf is including mod_php.conf, verify that this line is not commented. Also check that mod_php.conf is loading the PHP module:

Code:
LoadModule php5_module lib64/httpd/modules/libphp5.so
In mod_php.conf there should be an entry like this:

Code:
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
This tells apache to feed all files with a .php extension to the PHP module. Remember to restart the httpd service if you make changes to the configuration files.

You can also try the php script in the command line. Just save the php code above to a file and run it with php:

Code:
$ php script.php
Sometimes is also useful to use the var_dump() function to verify the script is doing what one expects.

Hope that helps.

Last edited by Diantre; 07-15-2011 at 02:21 AM.
 
Old 07-15-2011, 02:37 AM   #9
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Dear Sir,

I think my installation is messed up. See the below

[root@WAN-Admin httpd]# grep 'mod_php.conf' /etc/httpd/conf/httpd.conf
[root@WAN-Admin httpd]#

Since its not present do i need to install again ? i installed php using rpm through yum, do you think thats the issue ?

thanks alot for your support
 
Old 07-15-2011, 02:52 AM   #10
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by noony123 View Post
Since its not present do i need to install again ? i installed php using rpm through yum, do you think thats the issue ?
Take a look at your /etc/httpd/conf directory, type ls -lR /etc/httpd/conf, does the file appear in the list?

Use rpm or yum to view the contents of the PHP package you installed, and look for this file. It's also possible that the PHP configuration file has a different name.
 
Old 07-15-2011, 04:08 AM   #11
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Sir one thing also i forgot to mention

I am able to run few exec commands like cat, whois, who etc. But ping command is not working at all.

Also like you said, i did the following

php first.php

but it output the file as it is like cat. I didnt actually executed it

I will look for the file and let you know.

Thanks alot for your support
 
Old 07-15-2011, 04:36 AM   #12
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by noony123 View Post
I am able to run few exec commands like cat, whois, who etc. But ping command is not working at all.
If you're able to do that, then your apache and PHP configuration is probably fine. However it's strange that the ping command doesn't work. Try using the passthru() function instead of exec(). Have a look at the function pages in php.net, there are plenty of examples of use and good documentation.

Quote:
Originally Posted by noony123 View Post
Also like you said, i did the following

php first.php

but it output the file as it is like cat. I didnt actually executed it

Make sure the script code in first.php is inside the PHP tags <?php ... ?>. If there are no PHP tags in the file, it will happen what you describe, the text is printed, nothing is executed.

As a suggestion, use [code][/code] tags around your code, it keeps formatting, making it easier to read. For php code use [php][/php] tags, keeps the formatting as well and does syntax highlighting.
 
Old 07-15-2011, 04:54 AM   #13
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Dear Sir,

This is the update. This is my first.php file executed using php command

Code:
[root@WAN-Admin html]# cat first.php 
<html>
<head> <title> PHP </title></head>
<body>
<?php
echo "test";
passthru('/bin/ls /root');
?> 
</body>
</html>
[root@WAN-Admin html]#
Code:
[root@WAN-Admin html]# php -q first.php 
<html>
<head> <title> PHP </title></head>

<body>

test22202.txt
22202.txt~
23303.txt
All1
All2
All3
All4
All5
All_branches1
All_Branhces.txt
anaconda-ks.cfg
asterisk1.8
backup
VMwareTools-6.0.2-59824.tar.gz
vmware-tools-distrib
WHMONITOR.LST
</body>
</html>
[root@WAN-Admin html]#
As you can see, passthru is working perfectly. But when i open the same page in browser locally, it shows nothing. I think i am very close to solution now thanks to your support. If you could pls guide me, why its not working on the browser, does it has to do with the missing mod_php.conf file ?
 
Old 07-15-2011, 05:11 AM   #14
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Sir, please see below

Code:
<html>
<head> <title> PHP </title></head>
<body>
<?php

echo "test";
echo "<p>";
passthru('ps -e | grep http');
passthru('cat /test.txt');
passthru('ls /root');
echo "<p>";
echo 'new';

?> 
</body>
</html>
And the output on mozilla is

Code:
test

3182 ? 00:00:00 httpd 3184 ? 00:00:00 httpd 3185 ? 00:00:00 httpd 3186 ? 00:00:00 httpd 3187 ? 00:00:00 httpd 3188 ? 00:00:00 httpd 3189 ? 00:00:00 httpd 3190 ? 00:00:00 httpd 3191 ? 00:00:00 httpd asdas asda das dasd asd adas da d ad sad asd text file

new
As you can see, "passthru('ls /root');" is not working. i am surely missing something technical, when i run it via php, it works fine, but it doesnt show up on webpage. What can be the issue here ?

Please guide me
 
Old 07-15-2011, 05:26 AM   #15
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
I have verified that all my ls commands are working perfectly if i run my php script from cli. But it doesnt work if i open it via webpage. What possibly the issue ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How write a literal bash command in a bash file? xeon123 Linux - Newbie 6 11-29-2010 12:05 PM
getting php to run a bash command kitesurfa Programming 1 06-30-2010 02:58 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Automatically append another piped command to issued command in bash amateen Programming 1 05-07-2009 06:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration