Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2007-12-03
8
1,732 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 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