Running Python scripts from using PHP exec() - php

I want to run a python script in a button press event of a php/html file. I tried following. Is this possible with PHP exec()? But following didn't work. What is wrong with my code?
HTML file
<html>
<head></head>
<body>
<H3>Run Python Script...</H3>
<form name="input" action="test2.php" method="get">
<input type="submit" value="Submit">
</form>
</body>
</html>
PHP exec()
<html>
<head></head>
<body>
<H3>Executing...</H3>
<?php exec("first.py"); ?>
<H3>Completed...</H3>
</body>
</html>

exec Executes a command as if on the command line, running first.py just via bash won't work. You have two options:
Option A
You can tell the python parser explicitly to run the file with:
exec('python first.py');
Option B
Make sure your python script's first line is the path to python:
#!/usr/bin/python
#Python code...
Then call exec('./first.py')
(If you chose this option, you'll need to make sure the script is executable)
Conclusion
I'd probably go with option A as it's simplest and gets the job done without fuss.
Hope this helps :) x

There can be several problems with even your python script.
First: Can you run this script from console? (Do you have a #!/path/to/env/python in the script's beginning)?
If not, then either add it to your script or to the exec function (without #!)
You can also use passthru instead of exec, so you can display the raw output of the script.

try exec("/usr/bin/python path/to/first.py")

Related

I use php exec another php but this not working

i have this file:
pippo.php
exec(ExportExcel.php);
ExportExcel.php
#!/usr/local/bin/php
<?php
$stop=1;
......
php code
......
?>
The exec command runs the file but I don't know what it is doing. It remains hung up and I don't get to activate the breakpoint placed in the file.
Any idea?
Thnak's
You need to pass a string to exec(), so maybe try exec('./ExportExcel.php');
Reference: https://www.php.net/manual/en/function.exec.php

Using php exec() to run a C program on button press

I'm building a web app that has to run command line tools that all work fine while SSH'd in. Trying to use the exec() command to call the program I want to run on button click but no luck. Code below.
<?php if (isset($_POST['setup'])) { exec('home/ubuntu/pathtoprogram'); } ?>
<form method="post">
<button name="setup">Click to setup</button>
</form>
Is there something I'm missing or a mistake calling the program in the exec() function?
You should have this C program inside your project directory. Always use absolute path and it should works fine

HTML not showing characters

I'm running lighttpd 1.4.35 on Debian 8.2. I have a very simple html file with php code (php5) which calls a bash script and prints the output:
<html>
<body>
<?php
$res = shell_exec("/var/www/html/run.sh");
echo $res . " is the result";
?>
</body>
</html>
If that html file is called on firefox, the output is
is the result
If I directly run php with that file (php index.php), the output is
<html>
<body>
13.00
is the result</body>
</html>
So, where does the result get lost?
edit:
The webpage source code from firefox is
<html>
<body>
is the result</body>
</html>
edit: solved. bash script uses '~' which expands to wrong directory when the script is run from webserver.
The exec functions "only" return the contents of stdout, which is why you might miss an error message.
But you can redirect stderr to stdout, e.g. via
$res = shell_exec("/var/www/html/run.sh 2>&1");
shell_exec does not run if you're in safe mode, that could be the issue

Executing shell commands on a HTML gui - on local machine only

So I'm working on an application of my own where i need to execute shell commands, and even execute csh scripts on button clicks.All the HTML code is doing is displaying results from some sensors i have set up on my arduino via firefox, so im basically using it as a GUI. The HTML page wont ever be online, its purely running on my own machine.
I understand that its not straight forward as having HTML run shell commands on a live webpage is a security issue, but i have been reading about being able to execute shell commands using php scripts, but as it is running on a standalone PC i dont know how to integrate it into running a command from a button click on the page.
Infact i have no experience in using php at all, and i wouldnt even know where to start getting it to run through my HTML code.
Help!
edit:
If not php, is there any other way i can run a command such as 'ls' from a button click on the html page?
Oli
You can use the exec php function, and execute your scripts like this exec('/path/myscript.sh')
But surely you have to setup your php/apache in a correct way. Just try to look for some basic web-server configuration tutorials.
php command exec should execute a command and retrieve standard output.
<?php
$result = exec("wc -l /etc/issue");
print($result);
?>
With that print command you can print any html-text, that will be inserted into the page the client receives and displays. So simply surround that php-snippet with you actual web page.
General idea, you can create a HTML form with buttons that correspond to specific shell commands ( I understand this won't be LIVE on the web, so the security risks might not be an issue).
From there, you would receive the form input in PHP (you can use a framework like Laravel which makes this much easier to handle frontend/backend communication - http://laravel.com/docs/4.2/html http://laravel.com/docs/4.2/requests)
A quick example with HTML and PHP:
<form id="arduino" method="post" action="{{ url('arduino/control') }}">
<input type="text" name="command"/>
<input type="submit">
</form>
<?php
Route::post('arduino/control', function () {
$command = Input::get('command');
exec($command);
});

Python script from php doesnt give any output

This is my php script
<HTML>
<head>
<h1>Hello</h1>
</head>
<body>
<?php
$command=escapeshellcmd('~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
$output=shell_exec($command);
echo $output;
?>
</body>
</HTML>
I get no output when I run it through the browser.
When I change the
$command=escapeshellcmd('whoami');
it outputs as NOBODY,
I granted permission to nobody for that python script still no output.
I am using xampp for linux and php script in htdocs/xampp.
Are you sure you don't need to change your command to run python?
$command=escapeshellcmd('python ~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
You also might try specifying absolute path - the ~ alias changes based on current user (which is the web server's user, not yours).

Categories