How to execute shell from php script - php

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.

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);

run shell command from 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";
};

PHP using ruby returning output (using metainspector)

I'm currently creating a php page which does a ssh call with a .rb (ruby) file.
rb file
require 'metainspector'
page = MetaInspector.new("www.hln.be")
puts page.image
When creating a php file with the following code (php):
$cmd = "ruby facescrape.rb";
$last_line = system($cmd, $retval);
echo $last_line . '
echo $retval;
this only returns value 1.
However 2 things :
When running the same command in ssh, it will print the page.image
correctly.
When i change the rb file and for instance set as last line
puts "test"
this value returns correctly with also print correctly with the aboven php code.
I don't get why printing the page.image works in ssh but won't work by using that php code.
Also tried using exec() instead of system().
Thank you in advance!
Kind regards,
Kurt Colemonts

is it possible to run assimp commandline tool with the php

I used following command to convert the 3d models with the assimp Assimp, and it is working fine on Windows:
assimp2json seaknight.obj seaknight.json
I need to know how can I run this command from the PHP? I know that there re functions to run the shell execution from PHP, but it didn't work and I don't get any error.
PHP code is used is follows.
system("D:\assimp2json-2.0-win32\Release\assimp2json.exe assimp2json seaknight.obj seaknight.json");
and another one is
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json.exe assimp2json seaknight.obj seaknight.json');
chdir($old_path);
Found it myself
working code is below
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json monster.blend monster.json');
chdir($old_path);
no need to include the .exe, after remove it the command worked

How to execute a bash file from php on a button click

Hi i m trying to execute a bash file which contains a command to run a script file. I had been trying all possible methods but nothing seem to work. I want to run the file and display the result on the web page.
the bash file contains arguments to execute the script. i tried with
system(./bashfile path);
shell_exec(./file path);
if(isset($_POST['submit'])) {
$output = shell_exec('./Users/file.sh ');
echo "<pre>$output</pre>";
but nothing happens. what other ways i could execute the .sh file, help appriciated
system(),exec(),shell_exec(),... functions might be disabled because of security.
Also on the phpinfo page of shell_exec you can find:
This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.
try:
$command = './Users/file.sh';
$output = '';
exec ( $command, $output, $return_var );
var_dump($output);
var_dump($return_var);

Categories