Run a script on a computer from php - php

I'm unsuccessfully trying to execute a shell command from php. The goal is to switch on/off my music player of my computer/server via internet (with my phone for example). Here is what I would be able to do :
I have a very simple file "play.sh" :
Code:
xdotool key XF86AudioPlay;
echo "switched";
if I run it ./play.sh, that works (music player switches on/off)
then I have an other very simple php file "play.php" :
Code:
<?php echo shell_exec("./play.sh"); ?>
These two files are in the main folder of my server which is a partition of my computer. (I'm using lampp) But music is playing from my computer.
But when I'm going on localhost/play.php, I can see "switched" that showed me the sh file as been executed, but the sound doesn't turn off .
I just tried exec(), shell_exec(), passthru(), system(), .. with ./play.sh and /opt/lampp/.../play.php the result is exactly the same, and there is no error message.. :/
But that's weird, I'm not sure, but I think that what I run on my computer side is not the same than what I run on my server side. I mean that it's like the sound was turning on/off on the virtual server but had no link with the sound of my computer. That could be an explanation.. but then, how to execute a script on my computer from internet/my server..?
Does someone have an idea to do that ? Is it a configuration of apache..?
Thanks !
EDIT :
Here is how I solved my problem :
xhost + local:www-data
Thanks for all your replies

It may be a permissions issue - keep in mind that when PHP runs a command, it's run as the user the webserver is running as, e.g. www-data or apache or whatever. If your music player is running as your own personal user, your script may not have the ability to change it when run as a different user.

Shell_exec works. Try to put in your .sh script absolute path to the executables
/usr/bin/xdotool key XF86AudioPlay;

Related

Webserver to trigger ssh shell script

I have googled this a lot but none of the results I have found worked for me. So far, I have only tried to do this with php, but cgi, javascript or whatever works is fine with me, as long as it gets the job done.
I would like to access a certain URL on my debian webserver. Once opened in the browser, this file shall execute the following shell commands. No buttons or links. If possible, I'd like to just open the URL, then have the script being started.
ssh user#192.168.189.12 <<'ENDSSH'
osascript ~/Desktop/Scripts/script.scpt
When running this as a regular .sh file it works fine. I have created lockkeys so that no password is prompted when connecting from A to B. What can I do to trigger this from, for example, the browser on my smartphone?
I am not trying to connect directly from any device to the Mac containing script.scpt. It is essential that the debian server triggers it and that it is executed by the webserver.
I just started learning about terminal comments, scripts and so on, so I have very basic knowledge of the subject. Please be patient with me.
Thanks in advance for your help :)
for simplicity I prefer to create a bash script. Let's call it
/var/NONwebroot/sshcoolstuff.sh
#!/bin/bash
ssh user#192.168.189.12 <<'ENDSSH'
osascript ~/Desktop/Scripts/script.scpt
make sure it is executable
<?php
exec('/var/NONWwebroot/sshcoolstuff.sh');
?>
Now I'd recommend putting some protection on that PHP script. Either limit who has access to it by IP address, or a password, or both.
here is a test bash script for you
#!/bin/sh
cat > test << EOF
Hello World!
This is my test text file.
You
can also
have
a whole lot
more text and
lines
EOF

Running solr server from php

Currently, I have to navigate to the example directory of solr and execute java -jar start.jar.
However, I want users to have the ability of running the server automatically by selecting an option. Lets say,
1.The user downloads the `solr` directory on his/her server.
2.Enters the location of the example directory he/she just downloaded.
3.Selects/Clicks an option saying "Start Solr server".
4. And the server is started.
Is this possible? I'm looking to do this through php.
If you can do it via command line, you can do it via php's exec() assuming that is not disabled on your server.
Note: This is a very dangerous function if you allow user input to go through it.
I think you need something like the following command to do this. Just add the path to the given command:
<?php
exec("nohup java -jar start.php > output.log 2>&1 &");
?>
nohup is important to decouple the solr process from your php request.
Your idea is dangerous, because you allow the user to enter any location to start their scripts. So you really need to think about path validation to ensure that only solr is started and nothing else.

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

PHP from commandline starts gui programs but apache doesn't

First, I read some threads by people with similar problems but all answers didn't go beyond export DISPLAY=:0.0 and xauth cookies. So here is my problem and thanks in advance for your time!
I have developed a little library which renders shelves using OpenGL and GLSL.
Last few days I wrapped it in a php extension and surprisingly easy it works now.
But the problem is it works only when I execute the php script using the extension from commandline
$php r100.php(i successfuly run this from the http user). The script is in the webroot of apache and if I request it from the browser I get ** CRITICAL **: Unable to open display in apache's error_log.
So, to make things easier to test and to be sure that the problem is not in the library/extension, at the moment I just want to start xmms with following php script.
<?php
echo shell_exec("xmms");
?>
It works only from the shell too.
I've played with apache configuration so much now that I really dont know what to try.
I tried $xhost + && export DISPLAY=:0.0
In the http.conf I have these
SetEnv DISPLAY :0.0 SetEnv XAUTHORITY /home/OpenGL/.Xauthority
So my problem seems to be this:
How can I make apache execute php script with all privileges that the http user has, including the environment?
Additional information:
HTTP is in video and users groups and has a login shell(bash).
I can login as http and execute scripts with no problem and can run GUI programs which show up on display 0.
It seems that apache does not provide the appropriate environment for the script.
I read about some difference between CLI/CGI but cant run xmms with php-cgi too...
Any ideas for additional configuration?
Regards
Sounds bit hazard, but basically you can add even export DISPLAY=:0.0 to apache start-up script (like in Linux /etc/init.d/httpd or apache depending distro).
And "xhost +" need to be run on account which is connected to local X server as user, though I'm only wondering how it will work as php script should only live while apache http request is on-going.
Edit:
Is this is kind of application launcher?, you can spawn this with exec("nohub /usr/bin/php script.php &"); .. now apache should be released and php should continue working in background.
In your console, allow everyone to use the X server:
xhost +
In your PHP script, set the DISPLAY variable while executing the commands:
DISPLAY=:0 glxgears 2>&1

Run PHP cronjob on hosting system

I wish to run a lengthy PHP script on a hosted Linux system (for example Godaddy or OVH). The script is supposed to be able to run for 1 or 2 hours. I suppose that would be a cronjob.
I have tried to make a Linux command call my PHP with following:
print `echo /usr/local/bin/php -q myScript.php | at now`;
But this does not return any feedback, and the script is not processed.
Using 'which PHP' I could check that the path to PHP is correct.
Also, other simple commands like ls -l give me proper output.
I am not sure how to further troubleshoot this, or if it is even possible.
How would you recommend to proceed to run my PHP script?
Update:
I have tried putting the whole path to the script and it made no difference.
Since I do not know how to edit cron on the hosting account, I am calling my PHP script by calling another PHP file. I have:
triggerScript.php:
<?php
print `/usr/local/bin/php /path/to/myScript.php`;
?>
and myScript.php:
<?php
print `ls -l`;
?>
Now I run triggerScript.php on my browser, and the page seem to load for a while but I get a browser error "Internet Explorer cannot display the webpage".
Did you say GoDaddy?
Check this out:
http://help.godaddy.com/topic/67/article/3547?locale=en
All the linux shared hosting I use/used has option to setup cron from control panel. You do not have to setup by running a command from php...
Godaddy supports cron, even with the lowest priced account. I use this features on some of my sites hosted on Godaddy.
You probably don't have shell access and cannot get real cron jobs.
Use one of the free or commercial "web cron" providers that regularly call one of your web php scripts. Combine that with a set_time_limit call in your php script so it does not get killed for runnning too long.

Categories