0

I am trying to build a system in Laravel from which I am able to call some python scripts. Starting with https://github.com/maurosoria/dirsearch, I have placed the python script with the same machine and tried to run on api call.

If I run shall_exec('ls -la'); it runs perfectly and returns me result. But when I run the following command, the execution ends and no output.

shall_exec("python3 dirsearch-master/dirsearch.py -u https://www.yahoo.com/ -e *");

Then I used https://symfony.com/doc/current/components/process.html and try the same steps

use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

$process = new Process("python3 dirsearch-master/dirsearch.py -u https://www.yahoo.com/ -e *");
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();

In this case also the $process->run(); returns me no result.

5
  • The documentation of the Symfony Process component tells you, that you have to pass in the parameters of the command as an array: new Process(['python3', 'dirsearch-master/dirsearch.py', '-u', 'https://www.yahoo.com/', '-e', '*']); Maybe that's the problem? Commented Jul 22, 2019 at 11:15
  • Can you please verify that the python file is exectuable and found by that relative path by running shell_exec('ls -la dirsearch-master/dirsearch.py');? Commented Jul 22, 2019 at 11:17
  • I ran shell_exec('ls -la dirsearch-master/dirsearch.py') works fine. Then I tried new Process(['python3', 'dirsearch-master/dirsearch.py', '-u', 'https://www.yahoo.com/', '-e', '*']); I get following error ERR > /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'dirsearch-master/dirsearch.py': [Errno 13] Permission denied I change chmod to 777, still same error Commented Jul 22, 2019 at 11:31
  • I changed ownership of the Python code and I also changed a few lines of code, I get more detailed error: Symfony \ Component \ Process \ Exception \ ProcessFailedException The command "'python3' 'dirsearch-master/dirsearch.py' '-u' 'https://www.yahoo.com/' '*'" failed. Exit Code: 2(Misuse of shell builtins) Working directory: ../public Output: ================ Error Output: ================ /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'dirsearch-master/dirsearch.py': [Errno 13] Permission denied Commented Jul 22, 2019 at 13:56
  • Possible duplicate of Running python script in Laravel, How to run Python Script in Laravel?, Execute Python script using Laravel?, etc. Commented Jul 22, 2019 at 17:00

1 Answer 1

2

You can use the Custom Artisan command if you want. Create a new Artisan Command by running php artisan make:command COMMAND_NAME. it will create a new Artisan command in app\Console\Commands directory of your project.

class Analytics extends Command{

    protected $signature = 'Run:Shell {path} {u} {e}';

    protected $description = 'Run Python Via Server Shell';

    public function __construct(){
        parent::__construct();
    }

    public function handle(){
        $output = shall_exec("python3 ".$this->argument('path')." -u ".$this->argument('u')." -e ".$this->argument('e'));
        $this->info($output);
    }
}

Now, you can run the artisan command as php artisan Run:Shell path='dirsearch-master/dirsearch.py' u='https://www.yahoo.com/' e='*' and the output will be printed to CMD. You can also run this Artisan command from any controller you want as,

// Taking Inputs from Request in Controller
Artisan::call("Run:Shell", ['path' => $request->input('path'), 'u' => $request->input('u') , 'e' => $request->input('e')]); 

In fact, you can make the entire wrapper to run the python on your server-side using Laravel. But you should have the python3 installed and configured on your server before running it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.