I am trying to execute a shell script containing python commands from PHP using exec function in Ubuntu 18.04. The issue is Python script is not able to create a file for writing data.
This is my shell script test.sh
python3 p1.py
This is my python script
with open('trial.txt','w') as file:
file.write('Hello world')
This is my PHP script
<?php
try{
$out=exec('test.sh',$output,$status);
print_r($output);
}
catch(Exception $e)
{
echo $e;
}?>
I am getting $status as 1. Is there any problem with apache user permissions? Current apache user is www-data. I have given permissions to python script using chmod +x p1.py. This shell script works correctly and creates a new file if it runs from ubuntu terminal. But when executing from PHP, it doesn't work properly.
Related
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
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.
Ruby is installed here:
.rvm/rubies/ruby-2.2.1/bin
Ruby script:
puts "Hello world"
Php script:
<?php
$cmd = "ruby /home/balint/rubytest.rb";
echo system($cmd);
?>
I can run sudo php /home/name/public_html/phprubytest.php from CLI but not from the browser.
I reach the server via Putty and use Filezilla to put all my website-related files to the public_html folder.
I logged out the error and it turned out I have a permission error:
ruby: Permission denied -- /home/balint/rubytest.rb (LoadError)
This means as a user running the php script from the browser I have no access to that directory on the server.
Any ideas?
exec(rubyfile.rb);
Just add this code and it will execute your ruby file.
I have a series of shell commands I want to put in a program and execute the program from the command line. I decided to use PHP for this so currently I am trying to get the most basic shell commands to run.
Save as build.php
<?php
shell_exec('cd ..');
echo "php executed\n";
?>
From the command-line
php build.php
Output
php executed
Php executes correctly but I'm still in the same directory. How do I get shell_exec( ... ) to successfully call a shell command?
You need to change the cwd (current working directory) in PHP... any cd command you execute via exec() and its sister functions will affect ONLY the shell that the exec() call invokes, and anything you do in the shell.
<?php
$olddir = getcwd();
chdir('/path/to/new/dir'); //change to new dir
exec('somecommand');
will execute somecommand in /path/to/new/dir. If you do
<?php
exec('cd /path/to/new/dir');
exec('somecommand');
somecommand will be executed in whatever directory you started the PHP script from - the cd you exec'd just one line ago will have ceased to exist and is essentially a null operation.
Note that if you did something like:
<?php
exec('cd /path/to/new/dir ; somecommand');
then your command WOULD be executed in that directory... but again, once that shell exits, the directory change ceases to exist.
I am stuck with a problem in php for the last 3 days. couldn't find a solution yet.
I have a Cent OS remote machine and an Ubuntu local machine. I have a php script named test.php in my local machine so that I want to run some Linux commands in the remote machine using that php script. I used phpseclib for connecting to remote machine. The following is the php script test.php.
<?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SSH2('10.3.2.0');
if (!$ssh->login('makesubdomain','abcdabcd')) {
exit('Login Failed');
}
echo $ssh->exec('/usr/local/listdomain.backup/test/makedir.sh');
?>
I can't use root user here since root login has been disabled in remote cent os machine.
So I created this makesubdomain user and gave sudo privileges, that too without password by adding makesubdomain ALL=(ALL) NOPASSWD: ALL in /etc/sudoers file.The below one is the shell script which resides in 10.3.2.0
sudo -H sh -c '
mkdir /usr/local/testdir
if [ $? -eq 0 ];then
echo "success";
else
echo "not success";
fi
'
But now when I run the php script from terminal using command php test.php it showing error sudo: sorry, you must have a tty to run sudo. Ultimately, What shall I need to do with test.php and makedir.sh for creating a directory testdir as specified in .sh file using the given php script with user makesubdomain. Please advice as I am a very beginner in php.
(Note : I can run the makedir.sh file successfully in the remote machine, with the command sudo ./makedir as user makesubdomain, that too without prompting sudo password)
EDIT
I had commented Defaults requiretty in /etc/sudoers as given in http://www.unix.com/shell-programming-scripting/201211-sudo-sorry-you-must-have-tty-run-sudo.html, and it is working fine. Can i have any other option without doing this ?
Try calling $ssh->enablePTY() before doing $ssh->exec().