ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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
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:
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.
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.
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
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.
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 ?
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 ?
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 ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.