Solved

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

Posted on 2007-12-03
8
1,726 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
Comment Utility
works perfectly on my machine, is the ping command the same in Redhat?
0
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
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
Comment Utility
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
 
LVL 24

Accepted Solution

by:
glcummins earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Geoff Millikan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now