Browser Hangs Executing PHP that runs a Powershell - php

I am trying to run a simple PHP that runs a powershell script,
if I use this code I get the results in a command window, but I get an empty array in the browser:
<?php
exec("powershell C:\\Inetpub\\wwwroot\\my_shell.ps1 < NUL", $output);
echo "<pre>";
print_r($output);
echo "</pre>";
?>
I believe the NUL will discard the output, however it works in the browser found it on [this Fourm][1]
If I use this code, without the NUL I will get the results on a command window but if I run the script in the browser will keep loading forever and it will never give me any results:
exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1", $output);
Same results if I do it this way:
$output = shell_exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1");
The powershell Script runs fine if I ran it independently:
$cmd = "cmd.exe";
&$cmd "/C echo update tasks set last='manual' where id='8'; | sqlplus vvv/www#xxx";
So I need to execute this in a browser and get the output.

The powershell process isn't terminating, which causes the browser to hang. Add the following to the end of your script
Stop-Process -processname powershell*

Related

PHP shell_exec not producing desired output

I'm trying to run a java program and return the output then use that on my page.
if ($program) {
$output = shell_exec("java ftoa 12");
var_dump($output);
}
For some reason, that var_dump puts out null. I have ftoa.class in the same directory by the way. Also, the server my school is using runs an older version of php, not sure which one. Also when I run the command from the php command line, it works fine.
$:php -a
php>$output = shell_exec("java ftoa 12");
php>echo $output;
php>(desired output)

retrieve R output in PHP

Here's the issue:
I am using R to run some statistical analysis. The results of which will eventually be sent to a an embedded swf on the user's client machine.
To do this, I have PHP execute a shell script to run the R program, and I want to retrieve the results of that program so I can parse them in PHP and respond with the appropriate data.
So, it's simply:
$output = shell_exec("R CMD BATCH /home/bitnami/r_script.R");
echo $output;
But, I receive nothing of course, because R CMD BATCH writes to a file. I've tried redirecting the output in a manner similar to this question which changes my script to
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R /dev/tty');
echo $output;
But what I get on the console is a huge spillout of the source code, and nothing is echoed.
I've also tried this question's solution in my R script.
tl;dr; I need to retrieve the results of an R script in PHP.
Cheers!
If it writes to file perhaps you could use file_get_contents to read it?
http://php.net/manual/en/function.file-get-contents.php
Found it, the answer is through Rscript. Rscript should be included in the latest install of R.
Using my code as an example, I would enter this at the very top of r_script.R
#!/usr/bin/Rscript --options-you-need
This should be the path to your Rscript executable. This can be found easily by typing
which Rscript
in the terminal. Where I have --options-you-need, place the options you would normally have when doing the CMD BATCH, such as --slave to remove extraneous output.
You should now be able to run your script like so:
./r_script.R arg1 arg2
Important! If you get the error
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
could not find function "is"
You need to include the "methods" package, like so:
require("methods");
Perhaps,a much simpler workaround, would be:
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R > /dev/tty 2>&1');
echo $output;
Redirects both STDOUT and STDERR, since R outputs to STDERR, by default.

Trying to execute a C program from PHP and output to a web browser

I have a C program which has been compiled to an executable. I can run this program from my console. I am trying to get the output from this program through my web browser so I am using the exec command in PHP. My PHP script works fine when I execute it from the command line, but when I call it over a browser I get no input. Here is my PHP program
<?php
echo exec('/var/www/html/./readcard');
?>
The readcard program has 777 permissions. I am guess the issue is something to do with permissions?
You aren't capturing the output. The second argument to exec consists of an array to which the output lines will be placed.
<?php
$output=array();
$rv=0;
exec('/var/www/html/./readcard',$output,$rv);
if($rv!=0)
{
die("readcard failed, got a return value of $rv\n");
}
foreach($output as $line)
{
echo("<p>$line</p>\n");
}
?>
You probably just echo the return code of the script, which is zero. You can either redirect the output to a file and then serve that from php, or pipe the output stream directly back to php code.
Try
<?php
$output = array();
exec('/var/www/html/./readcard', &$output);
?>

how to hide system() output

i am working on windows XP . i can successfully run a system() command through my browser by calling a TCL script that automates a ssh session. I also return a value from the script. however my problem is that the script dumps the entire ssh session in the browser.
my php script looks like :
$lastline=system('"C:\tcl\bin\tclsh.exe" \path to file\filename.tcl '.$username.' '.$pass,$val);
filename.tcl:
spawn plink -ssh $user#$host
expect "password:"
send "$pass\r"
expect "\prompt:/->"
set $return_value [string compare /..string../ $expect_out(buffer)]
/...some code...this runs fine/
exit $return_value
everything runs fine and i get $return_value back correctly but the php file prints the result of the execution of the entire ssh session in my browser which looks like:
Using username "admin". admin#10.135.25.150's password: === /*some text*/ === \prompt:/->.../some text/
i want to prevent the system() function from printing this in my browser
i have used the shell_exec() function but it returns the entire ssh session result (which i have parsed in the tcl script and got a precise value to return to the php script)
is there a way i can do this without using shell_exec() but using system() instead
thanks in advance
The documentation for system() specifically says:
Execute an external program and display the output
On that page are listed alternatives. If you use the exec function instead, it will only execute the commands without displaying any output.
Example:
<?php
echo "Hello, ";
system("ls -l");
echo "world!\n";
?>
will display the output of system:
$ php -q foo.php
Hello, total 1
-rw-r--r-- 1 bar domain users 59 Jul 15 16:10 foo.php
world!
while using exec will not display any output:
<?php
echo "Hello, ";
exec("ls -l");
echo "world!\n";
?>
$ php -q foo.php
Hello, world!
use ob_start(); before and ob_clean(); after calling it
http://sandbox.phpcode.eu/g/850a3.php
<?php
ob_start();
echo '<pre>';
$last_line = system('ls');
ob_clean();
echo 'nothing returned!';
?>
In general if you want to prevent anything to output to the browser you can use ob_start() before your system() call and then ob_end_clean(). See http://php.net/manual/en/function.ob-start.php

issuing things to shell and forgetting about it in php

how do you issue commands to the shell and then forget it's output and such? for example:
<?php
echo `sleep 2;echo hi`;
echo "foo";
?>
the result for this is hifoo. i would want a result that gives me foohi. why? i want the command issued to the shell simply issued and forgotten, i am confused about why PHP will wait for the result. is such a result possible?
(the idea behind this is setting up the correct number of selenium grid RC instances programatically. currently, it will stop after the first process is opened)
From php.net exec()
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.
The same applies for all shell commands.
It's like anything you do at the command prompt, unless you take measures to put the new command in the background, the shell (and PHP ) will block until the command exits. Try this:
<?php
echo `(sleep 2; echo hi) &`;
echo 'foo';
?>
Note the brackets around your command, without that, the & woulud apply only to the echo , and you'd still have the 2 second pause.

Categories