Solved

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

Posted on 2007-12-03
8
1,731 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 24

Accepted Solution

by:
glcummins earned 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses four methods for overlaying images in a container on a web page
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 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