Raspberry Pi unable to execute script by PHP - php

I'm playing with Raspberry Pi and anm Arduino shield in order to run a script via Apache/PHP. This script simple blink a LED. I have already tested the script via shell and it works fine, with the command
/root/arduPi/blink_test
I'm able to see my LED blinking. So I made the same thing via Apache PHP with this short PHP script
<?php
if(isset($_GET['cmd'])){
echo '/root/arduPi/'.$_GET['cmd'];
exec('/root/arduPi/'.$_GET['cmd']);
}
?>
but nothing happen and no error has been displayed.
I tested the PHP code with
<?php
phpinfo();
?>
and it's fine. How can I fix this problem?

I had the same issue a while back it was because Apache does not have permission to access certain devices on the Pi. I fixed this by getting rid of the need to be root to access these devices. HERE Is my post about this same issue the fix was to setup sudo as passwordless.
THIS is what I used to accomplish setting up sudo as passwordless. You should then be able to execute the script as follows exec('sudo /root/arduPi/'.$_GET['cmd']);

Related

How to use PHP to execute AutoHotKey script on Windows Server 2016?

I have a Windows Server 2016 VPS with Plesk and PHP 7.1x.
I am trying to execute a simple AutoHotKey script from PHP using the following command:
<?php shell_exec('start /B "C:\Program Files\AutoHotkey\AutoHotkey.exe" C:\inetpub\vhosts\mydomain.com\App_Data\myahkscript.ahk'); ?>
This is the only line on the page. I have tried different ahk scripts, the current one simply creates a MsgBox.
When I execute my php page, on VPS Task Manager I see three processes created with the expected USR: cmd.exe, conhost.exe and php-cgi.exe. However, my PHP page just sits waiting on the server and nothing actually happens on the server.
I have also tried the same line except replacing shell_exec with exec. This seems to make no difference. I have tried without start /b with both commands. In that case the PHP page completes but no new processes are started.
I cannot find any errors in any logs: Mod_Security, Plesk Firewall, IIS.
Any ideas?
EDIT:
I tried my command from the VPS command prompt and immediately slapped in the face with the obvious issue of the space in 'Program Files'. I quoted the string as shown above and the command works. This eliminated the hang when running from PHP. However, the command still does nothing when executed from the web page.
EDIT:
Based on suggestions from the referenced post 'debugging exec()':
var_dump: string(0)""
$output: Array()
$return_val: 1
One point was that I would probably not be able to invoke GUI applications. That puts a damper on the idea.

Php Shell Exec doesnt work

i've got a site on my raspberry pi with remote launching music.
The script in .php looks like this:
<?php $message=shell_exec("cvlc /home/pi/1373_128.m3u &");
header('Location:index.html'); ?>
It is working from a command line ( php5 radioon.php )
But when I am trying to launch it from website it is not, any ideas what may be wrong? The php script itself is fine, it is working when it comes to turn on light in room etc.

Running imagesnap in PHP with Mac Server

I am trying to develop a small site to take snapshots of my house using a webcam connected to to my mac mini at home. The mac mini is running OS X Server. The Webcam is a Logitech HD Pro Webcam C920 (USB connected). It is based on PHP and HTML5 (no other code besides CSS).
My development machine is a MacBook Pro using the internal iSight camera. It is not running OS X server; I manually enabled apache and PHP. While developing the site on my laptop, everything worked flawlessly. When I installed it on my mac mini the imagesnap program turns on the camera but never finishes; I have to manually kill the process, and the snapshot is never created.
The following error is logged:
imagesnap[2363]: *** QTCaptureSession warning: Session received the following error while decompressing video: Error Domain=NSOSStatusErrorDomain Code=-12903 "The operation couldn’t be completed. (OSStatus error -12903.)". Make sure that the formats of all video outputs are properly configured.
This is only when running it from the site directly. If I run the same program from the command-line, it works just fine... even running it as the _www user!
sudo -u _www bin/imagesnap images/snapshot_$DATE.jpg
This is the code that does the call in PHP:
<?php
if(isset($_POST['submit'])) {
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
}
?>
I was unable to figure out how to write the date to the output filename (I'm a noob with PHP), so I used a bash script to do it for me (I'm better with BASH):
#!/bin/bash
DATE=`date +"%m-%d-%y_%T"`
bin/imagesnap -q -w 3 images/snapshot_$DATE.jpg
I've also tried exec(), passthru(), and shell_exec() all with the same results.
Finally, I've also tried the following: Create test.php, put code:
<?php
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
?>
and ran it on the command-line as the _www user using the php-cli:
sudo -u _www php test.php
and it also worked just fine (created the snapshot file and did not give error).
I'm thinking maybe it has something to do with the OS X server app, but google searches return nothing.
I'm stumped. Why does it break when I run the same exact code on the browser but works fine on the command-line using the same exact tools and user?
Unknown if this is the correct way, but I ended up using sudo to run the script.
visudo
and then give the _www user passwordless root access to the script:
_www ALL=NOPASSWD: /path/to/script
and now it works. There's always more than one way to skin a cat..

R script in php

I am running the php script containing Rscript in terminal. its working fine and giving the output. but the same script if i call from the webpage its not giving any output. i have checked the folder permission also. please help me to fix this.
my php script:
<?php
$abc=exec('Rscript dist/GS_R2html.R');
print "$abc\n";
?>
Rscript:
a<-rnorm(1000)
library(R2HTML)
setwd('/var/www/test')
HTML.title("Data Summary", file="dist/gshtml.htm",append=F)
HTML(summary(a),file="dist/gshtml.htm",align="left")
HTML(matrix(a[1:100],nrow=10,byrow=T),file="dist/gshtml.htm",align="left")
HTML.title("Histogram", file="dist/gshtml.htm")
jpeg("dist/plot2.jpg")
hist(a,col="red")
HTMLInsertGraph("dist/plot2.jpg",file="dist/gshtml.htm",Align="left")
dev.off()
Its due to the missing of the rApache which supports web application development using the R and the Apache web server. I followed the steps mentioned in the http://rapache.net/manual.html to install and configure R in Apache.

Server side script for launching application

I have been trying unsuccessfully so far to write a php script that will run when a page is opened and that will launch metasploit!
I ve tried shell_exec and exec and all the other alternatives but although I can get it to do simple things (i.e. ls, cds etc) if I try msfconsole it doesnt do anything!
I have also tried a different script that launches firefox and again nothing happens!
Now i know that php runs on the server and I m not expecting to see a console or firefox opening in the clients machine! Instead in order to check if it works I am trying to echo out the output of the shell_exec!But anyway since im hosting the files on my machine (i.e. this is the server and a VM is the client) if it could actually launch firefox i should be able to see the app opening here in the same way as by just doing this from the command line!
What am I missing?
Is there any other way to do this?(i.e. Launch metasploit everytime a user opens up my page)
NOTE: I've tried specifying the full path for msfconsole but that didnt work either!
Heres what I have so far:
$output = shell_exec('/opt/local/libexec/metasploit3/msfconsole;show');
echo "<pre>$output</pre>";
The ";show" bit was used in order to actually make it run something and print some stuff but didnt make any difference!
When you run a gui application from the command prompt in a X window system, it will use the default display. When you run it using php which is embedded in apache webserver, the program may not know where to display the gui application.
there are 2 things to make this work.
The program that executes the gui application must have permission to use display
you need to tell the program which display to use.
I used the following in my php script
<?php
$cmd = `export DISPLAY=:0; gedit`;
shell_exec($cmd);
?>
and ran the script from terminal using php -f test.php
I got the gedit up and running.
You can test the same with the script in apache too.
Please add apache user with privileges to access display server
update: I just added the following in /etc/apache2/apache2.conf (I am using ubuntu)
User poomalai
Group poomalai
and restarted the web server
sudo service apache2 restart
now I accessed localhost/test.php
and Presto!! I got the gedit :)
Hope this helps

Categories