...

View Full Version : capturing program output



nero0102
10-09-2004, 02:49 AM
can you capture a programs output and display it on a web page in php?

Just today I saw that

<?php
$output = shell_exec('who -b | cut -c25-');
echo "Server's last reboot: $output";
?>

would output the servers last reboot time, by executing the command in ()'s.
(was looking at my admin's .profile for how he got unix to spit out all the Hi blah blah, its blah blah time stuff when I logged in)

I thought this was super slick since I had been trying to do this for awhile in CGI/Perl.

Since the thing I want to do is a compiled program (using gcc to compile a .c file) would I have to pipe its output to a file and then use the shell_exec to print the contents of the file?

Also, is the php manual good for learning from, or a good resource?

The one book I have that mentions php only has one chapter, and Im too poor to buy any more books this semester :p

I guess I could go through my directories and remake everything in php for starters.

firepages
10-09-2004, 04:24 AM
your executable should just print to stdout , no need to save to file unless you have that specific requirement.

exec() basically returns stdout from whatever it is executing, if the output is multiline then PHP loads it as an array..



<?
exec('du -h' , $yaks);
print_r($yaks);
?>


executables that require further input from the user (in this case PHP) are another matter , you can use proc_open (http://www.php.net/proc_open) to `talk` to an executable but its easier said than done ;)

if an exec hangs or returns nothing (when you expected it to) redirect stderr to stdout
<?exec('some_command -switch args &2>1',$yaks);?>

nero0102
10-09-2004, 06:32 AM
First, thanks for the quick reply.

I tried your code substituting my program name and variable name.

and my HTML output was

Array()

I looked at the manual page for print_r, and im confused as you how that would handle say a paragraph of text.

would it be an array of strings? where each index would be a line ending in a \n ?

so the array would be

1 => Hello\n
2 => The sky is blue\n
3 => and cows go moo\n

Would I get the blank array output because this program is not from the admin ?

The program is just on my directory (same as my site, and is chmod to 755).

t

firepages
10-09-2004, 07:26 AM
chmod to 0755 should be enough for PHP to exec the file.

As far as I know PHP exec() simply turns output into an array based on newlines & print_r() is just an quick way to view that data.

did you try adding &2>1 to the end of the command string ?
<?$command = "/path/to/binary $args &2>1";?>

also note that exec() and shell_exec() are different

$output = shell_exec($command);
exec($command,$output);

nero0102
10-09-2004, 05:01 PM
Played around with it some and it works with shell_exec


<?php
$result = shell_exec('[filepath]/[program_name]');
echo $result;
exit(0);
?>


I can't believe how all these neat things I want to do are only 3-5 lines of code each.

Thanks for your guidance :)

nero0102
10-09-2004, 05:12 PM
well I shouldn't say it works, because my text comes out all wonky. The \n's in the program do not carry over so everything is smooshed.

I've tried concatenating the result with line breaks but it prints all the output at once then concat's whatever I put.

nero0102
10-09-2004, 06:21 PM
fixed it

went back into my C program abd out <br /> at the end of each line and recompiled.

Now my whole program executes and prints to the html with the line breaks included.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum