?
Solved

PHP system(ping www.example) > why doesn't this work?

Posted on 2007-12-03
8
Medium Priority
?
1,766 Views
Last Modified: 2008-02-01
I want the following to output results of the PING command on my web page.  Why doesn't the following work?
<?php
echo "<html><body><pre>";
system('ping www.example.com');
echo "</pre></body></html>";
?>

Open in new window

0
Comment
Question by:Geoff Millikan
8 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20401042
works perfectly on my machine, is the ping command the same in Redhat?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 20401060
A few possibilities:

 1. Name resolution is not working on the server. 'www.example.com' is not being resolved to a name. Make sure DNS is setup properly.
 2. Use of the ping command is restricted to certain users. Make sure the webserver user can execute ping.
 3. The ping command is not in the search path. Include the full path to ping.


A more complete output from the command would be helpful in narrowing down the source of the problem.
0
 

Author Comment

by:Geoff Millikan
ID: 20401102
Yep, the PING command works the same on all OS's as far as I know - FreeBSD, Ubuntu, RHEL and  Windows as far as I know.

In Red Hat 5, The PING binary is located at:
./bin/ping
./bin/ping6

From the directory the script is in I can run the below command and get results.  The fullt path back to PING is a long way up!  I put that path in the script and still no output at all.  No errors.  No warnings.  No nothing.

[root@mammoth dev]# ../../../../../bin/ping t1shopper.com
PING t1shopper.com (204.2.109.47) 56(84) bytes of data.
64 bytes from t1shopper.com (204.2.109.47): icmp_seq=1 ttl=54 time=65.8 ms
64 bytes from t1shopper.com (204.2.109.47): icmp_seq=2 ttl=54 time=65.8 ms
64 bytes from t1shopper.com (204.2.109.47): icmp_seq=3 ttl=54 time=65.8 ms


<?php
echo '<pre>';
system('../../../../../bin/ping t1shopper.com');
echo '</pre>';
?>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Accepted Solution

by:
glcummins earned 2000 total points
ID: 20401115
There isn't really a need to use a relative path. If ping exists in the top-level directory /bin, just use:

system('/bin/ping t1shopper.com');

Still need to check, though, that the webserver user has permissions to run the command. On some Unix/Linux boxes, permission to ping is restricted.
0
 

Author Comment

by:Geoff Millikan
ID: 20401116
ARRRG!!

It's SELinux.  It's banning httpd access to the PING binary.  Arg.  SELinux.  I have a love hate thing going with that thing.

Sorry guys, should have checked that first before posting.
0
 
LVL 16

Expert Comment

by:DrNikon224
ID: 20401120
You either need:

echo system('ping www.example.com'); // http://www.php.net/system

or

passthru('ping www.example.com'); // http://www.php.net/passthru

The `system` command will return results, if PHP is running as a server module. Do you, by chance, have it running as cgi? Using the `passthru` function should get you the results you're after.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 20401128
Gotta love SELinux. My first act on a new install is to kill it, and then restart. Sure, it's not as secure, but at least I can actually perform the tasks I need to perform!

It seems to follow the same mindset as the sys admin who requires passwords be fourteen characters long, with at least three non alpha-numerics. Sounds secure, but the passwords end up getting sticky-noted on monitors.
0
 

Author Comment

by:Geoff Millikan
ID: 20404029
I have PHP reunning as server module but I'm going to need forking capability soon so I think I'm going to need to install it as CGI too.  I've heard it runs less effcinetly under CGI.  Anyway, thanks for the passthru() suggestion.  I'll give that a shot.  

I have ping running on FreeBSD using the php-pear Net class at the link below but we're migrating to a new server under Red Hat and I want to write something from scratch that gives a bit more flexability.  Thanks for all the help.

http://www.t1shopper.com/tools/
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question