I am trying to call a python script from php which have proper execution permission but the script contains some of the commands for which only the root has the permission. So how can i make sure that those commands runs properly from the webservice???
I followed this link: Running a Python script from PHP but could not understand how to do it. If someone could explain, it will be a great help.
I will give you the usual warning regarding having PHP anywhere near root. I only do this because you mention this is a webservice (public facing?).
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//Setting the second argument in getShell():
//true will return a shell with root
//false will return a shell with the php execution user
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('python /full/path/to/python_script.py');
Related
I am trying to run a PHP script inside the ruby shell. While it is working perfectly if I am using the snippet directly in the ssh terminal, it is returning an error if executed with ruby:
zsh:1: command not found: php
Using this script below with commands like ls is working fine.
require 'rubygems'
require 'net/ssh'
host = "abc.de"
user = "user_xy"
pass = "user_pass"
begin
Net::SSH.start(host, user, :password => pass) do |ssh|
$a = ssh.exec! "cd xy_dir && php abc.phar do_this"
ssh.close
puts $a
end
rescue
puts "Unable to connect to #{host}."
end
How can I run PHP using Net::SSH?
Thanks for your help
I think the problem is not with Ruby per se, but probably with any language's SSH implementation. When using the language's ssh support to create an ssh session, it does not create a login shell (which would read initialization files such as .bashrc), but rather, a lower level interface to the machine.
Therefore, some functionality you would expect from normal shell use will be missing when connecting with Ruby's Net::SSH.
I was thinking there may be a way to get around this by calling bash -l -c "[the commands]", to force a login shell with bash's -l flag, and -c command specifier, but could not get it to work.
I did find this other SO issue whose answer discusses an awkward workaround that probably is not worth trying: ruby net-ssh login shell.
I am unable to execute a source command in linux using php.All other commands are working except this one. I need to execute the following command.
source /root/Envs/ate/bin/activate
This activates the ate-Automatic Test Equipment.Once I activate it then I need to run a python script as the script accesses the remote server.
I am able to manually run it but I am creating a tool which will automatically do it.
<?php
exec("source /root/Envs/ate/bin/activate", $output, $return);
echo "Command returned $return, and output:\n";
echo exec("python box_upgrade-pradeepa.py");
?>
The above commands returns 1 which means there is an error.But I am not sure how to run the 'source command'. The python script will run only if the source command is successful.(the python command is correct as I replaced hello.py and it ran fine.)
Could you pls help me as I am really stuck for a week?
Thanks a lot..
I found out the error. Since I am doing it using php (for a web tool) the user is Apache. 'Apache' user is unable to access the script in root folder. Moving it to another directory, I am able to run the script fine.
Thanks all..
I am trying to run a perl script from php that requires parameters to be passed to the perl script to run correctly. The following is the correct usage of the perl script from the linux terminal:
/home/user/test.pl -a alpha -b beta
or just
/home/user/test.pl -a alpha
I have execute permissions on the script and can run it without any parameters and the correct usage from the script is displayed back to my browser.
Below is the PHP code that works by displaying the usage back to my browser:
$result = shell_exec('/home/user/test.pl');
echo $result;
And the following is the problem code which I can not for the life of me figure out:
$test = $_POST['test'];
$result = shell_exec('/home/user/test.pl -a'.' '.$test);
echo $result;
Can anyone tell me what it is that I am missing to make this work correctly?
Thank you for the help.
My issue resided within the perl script itself and a specific line that was trying to output to a log file which the apache user did not have access to. I was calling the script correctly the whole time but once I was able to get to the server side logs (granted by system admin) I saw the issue was buried within the Perl script and not in php.
I need your help here.
I wrote one PERL script for PHP application which needs to be run for every 5 mins.
This script will call PHP program, which will fetch data from MySQL DB and will generate a excel report and will mail those reports to specific users.
Every thing seems to be fine when I ran this script manually with the command (perl reports.pl).
But when I set this Perl in a cron tab, nothing works and reports are not getting generated.
Details: perl script path /opt/app/deweb/web/EDI/Microsoft/reports.pl
this script will call PHP program (/opt/app/deweb/web/EDI/Microsoft/reports.php)
content of script
#!/usr/local/bin/perl
use Net::FTP;
use File::Copy;
use POSIX;
#errorreport = `php /opt/app/deweb/web/EDI/Microsoft/reports.php`;
print "#errorreport\n";
exit;
It is working perfectly when running Manually using command - perl reports.pl
No results, when set in CRON:
*/5 7-19 * * * /usr/local/bin/perl /opt/app/deweb/web/EDI/Microsoft/reports.pl
Please note that this crontab is under super user account named webserv and my login is having access to edit under this super user account.
I'm editing this cron tab using command :: sudo -u webserv crontab -e
I would check the following:
Does it run using sudo -u webserv perl reports.pl? If not, fix the problem for the webserv user (permissions or whatever) and it should work via cron too.
Does which perl using your login give you /usr/local/bin/perl? If not, change the path to Perl in crontab to what you got in which perl to fix the problem.
I found myself to be in the same situtation. After trying to find out the reason, I am almost sure about the reason this happens. Crontab does not have the same environment variables as you when running the script. You must be sure about paths. Try for example run your script like /perl-path /path-to-perl-script/script.pl outside the parent directory of the script and I am almost sure that your programm will not find some files. And as you call one php script from the perl script, it's possible to have the same problem with paths to your php script too.
So the solution is to use absolute paths and no relative.
Also at your perl script don't use php but /full-path-to-php for example:
#errorreport = /usr/bin/php /opt/app/deweb/web/EDI/Microsoft/reports.php;
I'm setting up a big system relying on python 2.7 being run through php. The call is always something like:
exec('python test.py');
However no matter what I do PHP keeps using python 2.4 for executing my files. Because of the size of the system I can't change in the programming, but will have to make 'python' point directly to python2.7.
By searching around I have reached the conclusion that I should change the php env.
echo getenv("PYTHONPATH"); // NOTHING
echo getenv("PATH"); // /bin:/usr/bin
I can do so through putenv (for example: putenv("PATH=/usr/bin/python2.7:".$_ENV["PATH"]), but php keeps running python 2.4 no matter what I change it to.
Hope somebody out there got a simple solution :)
Could you not just do this instead:
exec('/usr/bin/python2.7/python test.py');
another option, you can set path to interpreter in 1st line of script test.py
#!/usr/local/bin/python2.7
but you need make test.py executable
chmod +x path_to_file/test.py
and run from php as
exec('path_to_file/test.py');
P.S. be attentive administrators sometimes disable exec function on servers for safety.
disable_functions="popen,exec,system,passthru,proc_open,shell_exec" ....
If you can't use full path, try an alias:
alias python='/usr/bin/python2.7'
python --version
Python 2.7.2