How to run Python3 script from php on CPanel? - php

I have a python script that i want to run from index.php and they both are in the same directory.
The problem is that there's no output shown on the page(index.php).
I am running the page on a CPanel shared hosting from GoDaddy.
I have the permissions -rwx for both the files.
There's no error in running the python3 script file from the terminal.
The same files run smoothly on my computer's localhost but not on the CPanel.
Am able to run bash files using the same php code.
I have tried using system() in php to call the python3 script file but this doesn't seems to work.
index.php:
<?php
$comm = "python3 test.py";
echo $comm;
$output = system($comm);
echo "\n";
print($output);
?>
test.py:
print ("Hello")
Current Output:
python3 test.py
I expect the output to be:
python3 test.py Hello

I think you will need to use shell_exec to run your python code like that
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
and the first line in the python file should be
#!/usr/bin/env python
Don't forget to give your python file the privileges
chmod +x test.py

Related

Php cannot exec python script on - RPI3

I cannot at all execute a python script via php exec. This is my code
<?php
$output = shell_exec("python /var/www/html/socket/power_on.py");
$output = shell_exec("sudo python /var/www/html/socket/power_on.py");
$output = shell_exec("/usr/bin/python /var/www/html/socket/power_on.py");
echo $output;
?>
I've tried all of the above yet no luck the only command that seems to work that I've also tried is 'ls'
How to I give the user www-data access to sudo, as that may work. Since I've tried chmod 777 on the directory and file still no luck

Launch python file from PHP on LINUX server

I tried many solutions but nothing it works :
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
I launched indivdually them but in all cases, Firebug returned : '<pre>'
So I tried this code founded on Stack Overflow :
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
But firebug returned nothing.
My python file begin with #!/usr/bin/env python and if I launch it on server that works !
Do you knwo how can I launch my python file from PHP file ?
chmod will return 0 on success and > 0 on error.
Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.
You can test this by running:
$ /usr/bin/env python /path/to/your/file.py
So check your file permissions to check if the file is executable by the user that runs the php script.
Just to test, you can just print a few lines in your python file
#!/usr/bin/env python
print "test line 1"
print "test line 2"
Then if you have verified permissions and the correct use of python, you can do this in your php.
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
At first, Do you have enabled shell_exec/system/passthru commands in php.ini?
shell_exec("python /home/folder/python/mapfile_compress.py");
I think, it could be problem with your $PATH. Try something like: (use full path to python)
shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
In my case that's work if I write this code :
$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);

shell script with SFTP from PHP

I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}

Invoking a python script from a PHP script

I am running a PHP web script locally for testing purposes through terminal with command: php -S localhost:8000.
<?php $command = '/usr/bin/python /Users/Jupiter/Desktop/NC/www/createHarbourContainter.py'; exec($command); ?>
I am trying to call this python script that exists in the same directory:
#!/usr/bin/python
import os
import sys
save_path = '/Users/Jupiter/Desktop/NC/harbours/'
name = sys.argv[1]
def newHarbourContainer():
os.makedirs(save_path + name)
def main():
newHarbourContainer()
if __name__ == '__main__':
main()
This python script has chmod x+ set to it.
I am able to run the python script from the terminal :python createHarbourContainter.py
What I am unable to do is get the PHP function exec() to invoke the python script. Help needed!
In PHP you can use backticks ` to execute shell commands, so try this:
<?php
`/usr/bin/python /Users/Jupiter/Desktop/NC/www/createHarbourContainter.py`
?>
See http://php.net/manual/en/language.operators.execution.php for more info
So I have found a solution:
$output = shell_exec('python createHarbourContainer.py')
And to get back an output into the browser:
echo "<pre>$output</pre>";
I was running the server in PHP Development Server.
Using bash command: php -S localhost:8000 from directory where index.php is located.
The server was logging all input and output from browser interface.
I realized that when calling: /usr/bin/python from php the PHP Development Serverwould halt and open python interpreter.
And setting the whole path to the python script I wanted executed didn't work because the PHP script didn't have permission.
How this can help someone else in future.

running shell command from php with sudo

I want to run this code from php
echo <password> | sudo -S /usr/sbin/asterisk -rx "dongle show devices"
but it's not working. Can anyone help?
You can just use the 'backtick' character (`) around your shell string like:
<?php
$output = `command_goes_here`;
echo $output;
?>
Keep in mind this will only work if the shell_exec() function would work on that server, which could also be used in a similar way.
Use php function shell_exec or exec to execute shell commands
For more details
http://www.php.net/shell_exec
http://php.net/manual/en/function.exec.php
u can try by this if u want to run shell script command in php file
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
u can also try other way by create .sh file for shell script and run that .sh file by php function
$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";

Categories