exec() command in PHP file reporting process not found - php

I'm trying to run this command within a PHP file on a localhost webpage that runs on apache and PHP:
echo exec("/usr/bin/killall mplayer 2>&1", $result);
But it does not work, this command print_r($result); gives this output:
mplayer: no process found
Array ( [0] => mplayer(5003): Operation not permitted [1] => mplayer(5004): Operation not permitted [2] => mplayer: no process found )
Things I have tried are adding this to the /etc/sudoers
www-data ALL=(ALL:ALL) ALL
%www-data ALL=(ALL:ALL) ALL
This is a device that runs on an internal network in my home, so not a security risk.
I'm pretty sure it is a file/permission/ownership issue, but not sure what to do to allow it to run.
Any ideas? Thanks.

Related

Unable to execute system commands from PHP

I am trying to execute a couple of scripts by using a remote interface. The environment is Raspbian on a Raspberry Pi (although I will be using Debian later as well) running LAMP.
The files are test.php and test.sh in the root directory of the webserver (say example.com)
test.sh
#!/bin/bash
sudo pkill chromium-browse
sudo reboot
test.php
<?php
$output=null;
$resultCode=null;
exec("./test.sh", $output, $resultCode);
// $ouptut = shell_exec('./test.sh 2>&1'); //tried this too
// echo shell_exec("./test.sh"); // as well as this
echo "Returned with status $resultCode and output:\n";
print_r($output);
?>
Initially, I had used
chmod u+x test.sh
but got an error code of 126. So I did this:
chmod 777 test.sh
Now I get an error code of 1, but it still doesn't execute. I have also tried
sudo visudo
then added
pi ALL=(ALL) NOPASSWD: ALL
(pi is the current loggedin user)
Currently I am getting this:
Array
(
[0] =>
[1] => We trust you have received the usual lecture from the local System
[2] => Administrator. It usually boils down to these three things:
[3] =>
[4] => #1) Respect the privacy of others.
[5] => #2) Think before you type.
[6] => #3) With great power comes great responsibility.
[7] =>
[8] => sudo: no tty present and no askpass program specified
)
Note: I use sudo all the time at the command line without being asked for a password.
I do have another php file in the same directory that executes an actual system command successfully. It has this line:
$uptime = exec("uptime");
which works just fine, so I know system commands are possible. Is there any way to do this? I have seen other similar questions on SO and other sites, but none of those answers have worked for me.
Any help appreciated.

PHP exec(), shell_exec(), proc_open() & powershell working in CMD but not in browsers

Below scripts working fine in CMD window but not in browsers. Below scripts are in 'front.php', win10's command window command 'php front.php' doing nice but http://localhost/blahblah/front.php sucking me by not triggering 'back.php'. Spent almost half a night trying all possibilites include shell_exec, proc_open but all ending up with above issue. Basic permission, php.ini related issues are all taken care.
pclose(popen('powershell.exe "Start-Process php -ArgumentList \'back.php\' -WindowStyle Hidden"','r'));
echo exec('D:\Wamp\php\php.exe D:\Wamp\apache2\htdocs\MySite\admin\back.php > output.txt 2>&1 echo $!', $pid);
Update
Just to narrow down, I'm using Win 10 Admin user and all WAMP setup is in my local pc only, using PHP 5.6.x. Both front.php and back.php are in same working folder and front able to write to file but back.php not executed from browser. From CMD the same is working!
front.php:
<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
file_put_contents('timelog.txt', date('Y-m-d/H:i:s.u'));
echo exec('php back.php > output.txt 2>&1 echo $!');
// pclose(popen('powershell.exe "Start-Process php -ArgumentList \'back.php\' -WindowStyle Hidden"','r'));
?>
back.php:
<?php
sleep(5); // some delay
file_put_contents('timelog.txt', date('Y-m-d/H:i:s.u'). "\n");
?>
There are a lot of problems with this.
I notice that back.php does not send anything to stdout or stderr (unless there are errors with the file op).
Also, your two scripts are writing timestamps to one file, and they are not in append mode, so one will overwrite the other. Try logging to different file names, or use FILE_APPEND as the third parameter in file_put_contents().
Finally it is not clear what the echo should do in php back.php > output.txt 2>&1 echo $!. That looks like a syntax error to me.
To investigate the nature of redirection in the console versus a browser, I wrote these two files:
inner.php
<?php
// Writes something to stdout and stderr
echo "Stdout\n";
fwrite(STDERR, "Stderr\n");
outer.php
<?php
// Execs the inner file with and without redirection
exec('php inner.php', $output);
print_r($output);
exec('php inner.php 2>&1', $output2);
print_r($output2);
On the console I get this:
Stderr
Array
(
[0] => Stdout
)
Array
(
[0] => Stdout
[1] => Stderr
)
You can see in the first case, the stderr output is rendered to the console as part of the exec() output itself, i.e. it is less trappable unless we do the 2>&1 redirection.
I then span up a web server:
php -S localhost:10000
and visited the address: http://localhost:10000/outer.php.
I received this in my browser:
Array ( [0] => Stdout ) Array ( [0] => Stdout [1] => Stderr )
and the stray error appears in console output:
PHP 7.0.22-0ubuntu0.16.04.1 Development Server started at Sat Oct 7 11:01:43 2017
Listening on http://localhost:10000
Document root is /home/jon/Development/Personal/Missive
Press Ctrl-C to quit.
Stderr
[Sat Oct 7 11:01:56 2017] 127.0.0.1:54392 [200]: /outer.php
[Sat Oct 7 11:01:56 2017] 127.0.0.1:54394 [404]: /favicon.ico - No such file or directory
Thus, I think everything is working fine here.
You may wish to create a simple reproducible test case - with a large list of edits and comment updates, your question is becoming rather too convoluted to answer. There may also be value in explaining what you're trying to achieve, in case it's an X-Y problem.

Antiword returns empty array

I have this script,
<?php
exec('/usr/local/bin/antiword /var/www/html/rezyme_2015.doc', $output);
var_dump($output);
?>
but in browser I see:
array (size=0)
empty
I have in /usr/bin/antiword and I copy to /usr/local/bin/antiword. If I run in console:
php antiword.php
Output is
[0] =>
string(0) ""
[1] =>
string(52) "hello"
[2] =>
string(0) ""
In one forum, I saw the exact problem reported. The person that has this problem says that he solved this problem. Here is the quotation:
"Yes, it turned out to be the accessibility of the map files - they weren't world readable, once I changed permissions, it worked great. Thanks all"
However, I did not unnderstand how he solved the problem and how also I can reach and allow those map files of antiword.
And I found:
Maybe the user apache or www-data or nobody or whatever your webuser is
called, has no permissions to execute it?
You can simply check this by:
1) sudo su www-data (or whatever etc.etc see above)
2) call the script as you do in your exec() command
Or you blocked exec() in PHP.ini.
I not found in PHP.ini in /etc/php5/apache2/ and /etc/php5/cli/ this blocked exec() or exec()
And I run in my terminal:
sudo su www-data
This account is currently not available.
My question, why in my browser this script not correctly work:
array (size=0)
empty
When I get chmod 777 -R for file.doc then I have return array, but how do right ?
and what I need for fix this problem, step-by-step, whats create, whats change, help please.

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.

PHP/Apache error.log doesn't execute new_line characters, why?

For example instead of getting the following
post:Array (
"a" => "b",
"c" => "d"
)
I just get this:
post:Array (\n "a" => "b",\n "c" => "d"\n)
It's really uncomfortable to read this while debugging my code. So if you have any suggestion on why this couldn't work alright, tell me.
I am running it in a Windows7 Putty connected to an Ubuntu virtual server, which runs supposedly it's default Apache/PHP configuration. (well probably not, but as always nobody in the team remembers to have changed anything)
edit: Someone requested the code that writes to the error.log:
<?php
error_log(print_r(array("a"=>"b","c"=>"d"),1));
?>
The commands to view the error log are:
sudo tail -f /var/log/apache2/error.log
sudo vim /var/log/apache2/error.log
sudo cat /var/log/apache2/error.log
In all instances the problem occurs that \n is not executed as expected.
I also faced the same problem, but after spending a few minutes I got a solution.
When you do tail, use as below:
sudo tail -f /var/log/apache2/error.log | sed -e 's/\\n/\n/g'
If you want, you can create a file. Give it some name and paste the above command and place that in the /usr/bin/ folder.
For example
vi tailme
With the contents:
#!/bin/bash
tail -f /var/log/apache2/error.log | sed -ue 's/\\n/\n/g'
And put this in /usr/bin/. Now you can use tailme as a command.
In some cases (e.g. Mac) using Perl might work better:
tail -100f /var/log/apache2/error.log | perl -pe 's/\\n/\n/g'
The problem is caused when the Apache process can't write into the error_log file, so the syslog writes into the file instead. The syslog messes up the line breaks.
So just do:
chmod 777 error.log
This should solve your problem. Source.
When calling error_log, you can force PHP to log directly instead of passing it to Apache which is the default behavior. To do that, just specify 3 as the second argument and a file path as the third argument.
error_log("error message", 3, $logFileLocation);
For more information, check out PHP error_log documentation.
If you are viewing the output in the browser, try wrapping your output statement with the <pre> tags.
<?php
$post = Array("a" => "b", "c" => "d");
echo "<pre>";
print_r($post);
echo "</pre>";
?>
outputs to a browser a formatted array:
Array
(
[a] => b
[c] => d
)

Categories