run shell command from php , - php

I need to run a python program with command line argument from my php script
This is my code
<?php
$output=shell_exec('python /path/to/python_from_php.py input1 input2');
echo $output;
?>
I am getting the contents inside shell_exec function as the output.
I tried using only
$output=exec('python /path/to/python_from_php.py input1 input2');
I am getting only the second input as my output.
But what I want is I should be able to print the input contents through my python script. If I remove the "echo $output" line I am not getting any output.
My python code
import sys
a=(sys.argv)
for i in (sys.argv):
print i

this code gives you the output from shell
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
from this reference php also ensure php is not running in safe mode
but you could try just call the python script from php
<?php
$my_command = escapeshellcmd('python path/to/python_file.py');
$command_output = shell_exec($my_command);
echo $command_output;
?>

As OP wants to:
But what I want is I should be able to print the input contents
through my python script. If I remove the "echo $output" line I am not
getting any output.
should be used passthru, description
<?php
passthru('python /path/to/python_from_php.py input1 input2');
?>
Check, that you php user have access to run python and your python script.
Live demo is here

One option is to use passthru(), but if you don't want to manage the output buffer, and having the output in an array is desirable, you can add an output parameter to the exec command as so:
<?php
error_reporting(E_ALL);
$command = escapeshellcmd('/usr/bin/python /path/to/python_from_php.py arg1 arg2');
exec($command, $output);
for ($l=0; $l < count($output); ++$l) {
echo $output[$l]."\n";
};

Related

execute R script from php

I am trying to execute R script from Php. It works fine with the command prompt with this :
Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R
But the same is not working from PHP call using exec. Any suggestion please!
<?php
exec("Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R", $output);
print_r($output);
?>
print_r($output) displays only "Array()" no correct result ;
Those paths must be relative to the php working directory. Otherwise they must be fully specified:
Try:
$cmd = "C:\Users\YOURNAME\Documents\R\R-3.5.0\bin\Rscript.exe C:\Users\YOURNAME\Desktop\my_script.R";
exec($cmd, $output);

How to execute shell from php script

I want to execute this command from a php script
scrapy crawl example -a siteid=100
i tried this :
<?php
$id = 100;
exec('scrapy crawl example -a siteid= $id' $output, $ret_code);
?>
try this:
<?php
$id = 100;
exec('scrapy crawl example -a siteid=$id 2>&1', $output);
return $output;
?>
You actually need to redirect output in order to get it.
If you don't need the output, and just to execute the command, you only need the first part, like this:
exec('scrapy crawl example -a siteid=' . $id);
because you don't put the parameter inside the ' ', you put it outside, read about text concat in PHP.
Depending on if you need the output of the script there are different approaches.
exec executes a command and return output to the caller.
passthru function should be used in place of exec when the output from the Unix command is binary data which needs to be passed directly back to the browser.
system executes an external program and displays the output, but only the last line.
popen — creates a new process that is unidirectional read/writable
proc_open — creates a new process that supports bi-directional read/writable
For your scrapy script I would use a combination of popen and pclose as I don't think you need the script output.
pclose(popen("scrapy crawl example -a siteid=$id > /dev/null &", 'r'));
From the PHP Manual - shell_exec()
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
So in short, there is a native PHP command to do what you want. You can also google for exec() which is a similiar function.
phpseclib - Download from http://phpseclib.sourceforge.net/ and include it in your project.
include('Net/SSH2.php');
$ssh = new Net_SSH2("Your IP Here");
if (!$ssh->login('Your User', 'Your Password')) {
exit('Login Failed');
}
$id = 100;
echo "<pre>";
print_r($ssh->exec('scrapy crawl example -a siteid= $id'));
Hope this helps.

How can i get the print values in python script in php variables in php script?

I have a python script which do some processing over images and gives me text, the script is running fine in the console. Now i want to display the same text by using php in a browser.
When i use the below code, i get the proper values in php variables $f and $v when i exectute the code from console but when i execute the code from browser i don't get any value in php variables
exec('python /var/www/abc/abc.py', $f, $v);
echo $f;
echo $retval;
In python script: I have something like this
code...
code...
......
#in the end
print text
sys.stdout.flush()
How can i get the print values in python script in my php variables??
Make sure $f is an array before calling exec(), then print_r($f) should do the trick

Execute multiple shell_exec at once?

How do I execute multiple shell_exec at once?
Also how do I switch each process to view output from test.php file? I need to know what each process (php file) is doing.
Take a look, example:
index.php
<?php
shell_exec("php test.php");
shell_exec("php test.php");
shell_exec("php test.php");
?>
That does not work how I wanted to be, first shell_exec (test.php) has to be completed first and then it will go to next shell_exec.
test.php
<?php
$section = rand(123,123);
$x = 1;
while($x <= 50) {
print $section . ": " . $x . "\n";
$x++;
sleep(1);
}
?>
It does not output when shell_exec is executed but when I type php test.php manually in shell, it does output.
Or is there a way to open multiple shells?
This is a quick example, however I will be executing multiple PHP file to run CURL
shell_exec("php test.php& php test.php");
Your best bet is to have those scripts log output to a file and actively parse it after running shell_exec().

Problems executing Perl scripts from PHP

Trying to figure this out. I am trying to execute a perl script within php, using shell_exec() like so:
<?php
$output=shell_exec("./tst.pl > test.txt");
//$output=shell_exec("./tst.pl");
echo $output;
?>
It will not write output to a file using ">" filename.txt.
It will work if I execute without directing it to a filename as I can confirm this with echo.
Does this have to do with using ">"?
Permissions should be fine as I am able to run the same perl script on command line and direct to file. Any suggestions for executing this?
The output of "test.txt" will be used as input:
<?php
$data = array();
$InputFile = file("test.txt");
...
?>
It was definitely a permissions problem. Wrote the file out to /tmp and it worked fine.
<?php
$output=shell_exec("./tst.pl > /tmp/test.txt");
echo $output;
?>

Categories