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

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

Geoff MillikanAsked:
Who is Participating?
 
glcumminsCommented:
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
 
nizsmoDeveloperCommented:
works perfectly on my machine, is the ping command the same in Redhat?
0
 
glcumminsCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Geoff MillikanAuthor Commented:
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
 
Geoff MillikanAuthor Commented:
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
 
GarbsTheTurtleCommented:
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
 
glcumminsCommented:
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
 
Geoff MillikanAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.