Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No output from EXEC command.

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
Hi all...

I am trying to use the exec command to ping a server on the local network through PHP. The problem I have is that the result of the ping is not being passed back to the PHP when the script is executed from a browser.

The command I am using is simply:

<?php echo exec('ping bbc.co.uk -c 1'); ?>

This does not return anything at all. It works FINE when I run the file from the command line (php -f test.php outputs the result of the ping). Also the apache user has not issues running the ping command (sudo -u apache ping -c 1 works fine). This is really annoying me.

It is not the exec command across the board, because an exec('whoami') return "apache". This is really annoying me, any advice or help will be greatly received!

G

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
There may be a few things the case here.

First, are you allowed to use the exec function (in other words, if safe mode off and is the exec function not blacklisted in php.ini)? You could check this by sending an echo command through exec.

Second, ping may be terminal-aware (although I doubt it) or send its output to stderr (which I also doubt).

Please try the echo statement. It tells you if it is the exec function or the ping command that fails.

Also, there are a few execution functions in PHP. I think exec has an optional parameter that allows you to get all its output as an array. Is there any output in there?

Good luck!

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
If you read my post above, you would notice that I KNOW it's not the exec command that is failing...

It is not the exec command across the board, because an exec('whoami') return "apache".

I appreciate your help but obviously reading my post saves everyone time and effort!

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
DonQuichote's advice remains valid. php will often use different php.ini files for the cli to a sapi. the easiest way to tell what php.ini file is being used is to check the relative outputs of a call to phpinfo(); from each of the cli/sapi.

however exec() may well not be what you're looking for anyway. the return value of an exec() call is simply the last line output by the function. if this is a blank line then you will not see any constructive output. read the manual for exec(): you have two choices as to how to receive the output from the call. however i note that the same file (assumedly) does produce some coherent output when run from the command line: which again militates towards DonQuichote's first point.


 
I looked at your other posts and I'm making the assumption that your on windows ?
I tried a ping from the command line and I got an error that option -c is not recognised as a command parameter. (I googled for linux version and that has no -c either) I also got nothing displayed from php when I tried that (probabbly as exec only displays tha last line which is blank).
When I removed the -c 1 text it worked and the php also displayed stuff (from the command line version, I don't have a web server on this PC)
So you could try the -c option on your machine command line and see what happens ? failing that try with just a normal ping (perhaps to localhost) and work from there.
I would take on board jpadie's comment on the last line only being displayed and consider what you want your code to do (and question if the bbc are happy for you to ping them !)
Finall I'm not going to preach but I'd be a little more - how should I put this ? - considerate with people who are trying to help you.
 
Sorry, you are completely correct my question is very badly worded. I am working on linux and the -c 1 tells ping only to send one ping.

The EXEC command returns the last line, but I have also used the exec command that stores the output as an array to no avail (although the script works fine on a WAMP server on my windows PC with the -c 1 parameter removed). You need to tell linux a count or it will ping forever unlike windows that sends 3 pings as a default.

This is really really annoying me. As a work around I am simply requesting a file through the webserver that happens to be on each box, however we will be disabling that on the core switches and some of the other devices I wish to ping.

Thanks again for the help and apologies for my grouchiness above!

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top