shell linux don't execute commande - php

I have this command and it works fine on shell.
I want to run it with php but it does not work
what i missed :
edited :
$arg="arg1"; $arg2= "arg2";
echo shell_exec("sshpass -p ".escapeshellarg($arg)." ssh -t root#00.00.00.00 'asterisk -rx ".escapeshellarg($arg2)."'");
after this change no error on output but still commande not executed
edited :
the original commande is like this :
sshpass -p 'arg1' ssh -t root#00.00.00.00 'asterisk -rx "arg2"'
and i want to around the probleme with external parameters but it does not work

If the php is running on safe mode, it won't allow to execute shell_exec as it's described in the php manual:.
https://www.php.net/manual/en/ini.sect.safe-mode.php
You can disabled the safe mode for a certain folder as it's described in the Stack Overflow question:
How to turn off php safe_mode off for a particular directory in a shared hosting environment?

Finally I found a good solution for who still search :
I installed php-ssh2 with this link (i use debian 8, php7)
blog.programster.org
and in my case i use codeigniter i follow the link to implement this class
bitbucket.org
now i can execute any commande on shell without probleme
and the probleme is solved :)
Hope its help someone :)

Related

Where is the PHP path in CentOS 6.5 droplet?

Unfortunately I do not have access to the droplet via console, only by Vesta panel and I need to set up cron job.
However, I can not find the right path to PHP. Now that command line looks that and it does not work:
/usr/local/vesta/bin/php5 -c /home/admin/web/ -q /home/admin/web/mysite.com/public_html/crop_scripts/cron.php
Please tell me what options instead of "/usr/local/vesta/bin/php5" I can try?
what does this code show when you access it through your web server (assuming exec call is allowed)
<?php
$output = array();
exec('which php',$output);
var_dump($output);

Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line

I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

Uglify-JS won't run using shell_exec()

My question is similar to this:
Nodejs - .node_libraries/ for www-data
I've been trying all night to get php to execute uglifyjs. I've tried:
die(shell_exec('NODE_PATH="/home/app/nodejs/node_modules/"; export NODE_PATH; uglifyjs -o /home/app/public_html/js/profile.min5.js /home/app/public_html/js/profile.js'));
and
die(shell_exec('node /home/app/nodejs/node_modules/uglify-js/bin/uglifyjs -o /home/app/public_html/js/profile.min33.js /home/app/public_html/js/profile.js'));
and a bunch of other crazy combinations but nothing works. Everything works fine if I do it manually in the shell. Can anyone help please?
Have you tried looking at the following (I'll go for the obvious since you haven't said what you've already tried):
user apache/www-data is allowed access to /home/app : sometimes /home directories are closed off (chmod 0700)
Is safe_mode on your server in effect? From the command line, try:
php -i | grep safe_mode
Then, look at the output of:
phpinfo();
Which version of PHP are you using (might well be irrelevant)?

Can't execute external program through system()?

I tried to compile my latex file in php script, but it can't call xelatex.
In php script:
system("/usr/bin/whoami");
system("/usr/bin/xelatex foo.tex 2>&1");
output:
myuser
sh: 1: /usr/bin/xelatex: not found
But in my terminal:
$ /usr/bin/whoami
=> myuser
$ /usr/bin/xelatex foo.tex
This is XeTeX, Version 3.1415926-2.2-0.9995.2 (TeX Live 2009/Debian)
...(successful output)...
I run php as myuser, and pass system() absolute path. And I turn safe_mode off. Why can't I still execute external programs?
Finally I contacted my system administrator and found the problem. The machine is in a NFS, so the apache and login shell is on different machines. There is no xelatex on the machine where apache is running.
Check the permission of the directory from where you running you PHP code. check for myuser permissions
just for verification try it with root.
Hope this help
Are you sure it's not the file 'foo.tex' which it is unable to find? Try having the shell output to a file, e.g. system("/usr/bin/xelatex ./foo.tex > ./test.out"); and see what luck you get then.

Run pdftk by php on linux centos

I would like to run pdftk on my webserver. It's a Linux Centos with PHP 5.3.2.
When I connect it by commande line I do
pdftk --version
It's OK
pdftk A=p1-9.pdf cat A1 output p1.pdf
It's OK.
Now, I do this by php :
exec(pdftk A=p1-9.pdf cat A1 output p1.pdf)
It isn't OK. Why?? I search about the link of file, but it looks OK.
This doesn't work too :
exec(pdftk --version)
I install pdftk with this How do I install Pdftk on my server?
So what's wrong??
Thank for your help!
I've run into this issue before. Assuming that you're wrapping your commands string in quotes (as gioele noted), the issue may be that you need to set your path when running the system command. Try this:
$command = "pdftk A=p1-9.pdf cat A1 output p1.pdf";
system("PATH=\$PATH:/usr/bin/ && $command",$response);
if ($response===FALSE){
//there was an error, handle it
}
(I've added a little response handling there as well). If that doesn't work, check to see what path you should use (it will depend on where you installed PDFTK).
I believe you can also get the same result by using putenv("PATH=" .[your path]); and I've used system() here, but exec() should be affected in the same way

Categories