0

On a local linux server (Rapsberry Pi debian stretch with desktop), I am working on sending "audtool" commands to a running Audacious media player using php, exec and bash scripts. Audacious is autostarted when the server starts up with user "pi". I have apache2 and php set up and working on the server, and I can ssh to the server and run all the commands from the cli. I believe I have resolved the issues with running audtool (dbus and setting the right environment variables) and running the php on the command line works successfully. However when running the php on a webpage I get back a long string of information about apache2

I have spent several hours (getting on for a whole day) researching this on the web in order to get to this stage, so close I can almost touch it, but stuck on this last element. The example is to display the current song from a running instance of Audacious. Audtool requires a running dbus (looks for a display). Using exec or shell_exec I have no problems running bash commands such as whoami or ls.

The php page (cursong.php):

<?php
echo exec('/var/www/html/cursong.sh');
?>

The bash script (cursong.sh):

#!/bin/bash
##call current song

pid=`pidof audacious`
user=`ps -p $pid -o user=`
export `strings /proc/$pid/environ | grep DBUS_SESSION_BUS_ADDRESS`
sudo -E -su $user /usr/bin/audtool --current-song

(from here: https://redmine.audacious-media-player.org/boards/1/topics/1058?r=1059)

Output from command line: php -f cursong.php

Artist - Song Title (for example - so this works)

Output on webpage:

declare -x APACHE_LOCK_DIR="/var/lock/apache2" declare -x 
APACHE_LOG_DIR="/var/log/apache2" declare -x 
APACHE_PID_FILE="/var/run/apache2/apache2.pid" declare -x 
APACHE_RUN_DIR="/var/run/apache2" declare -x APACHE_RUN_GROUP="www- 
data" declare -x APACHE_RUN_USER="www-data" declare -x 
INVOCATION_ID="4ce76136ca8842bd9108d6b1b9a5b9ed" declare -x 
JOURNAL_STREAM="8:23896" declare -x LANG="C" declare -x OLDPWD 
declare -x 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 
declare -x PWD="/var/www/html" declare -x SHLVL="1"

I have set www-data, the apache2 user with the following in /etc/sudoers:

www-data ALL=NOPASSWD: ALL

and /var/www/html is rwx for anyone

Obviously, I am expecting to see "Artist - Song Title" on the webpage, but instead I get back all the apache2 info. What am i missing, or where have I gone wrong?

Metric Rat
  • 196
  • 1
  • 3
  • 12

2 Answers2

0

I hate answering my own question, makes it look like I wasn't trying hard enough! After a further five hours or so of searching around and attempting fixes, I happened upon this post on SO:

Running command-line application from PHP as specific user

which suggested putting a "sudo -u user" in the exec of the php file. I tried this with the "pi" user and it still didn't work, then I simply tried it with "sudo" and hey presto!!

The php file now looks like this:

<?php
echo shell_exec('sudo /var/www/html/cursong.sh 2>&1');
?>

Now to do some testing on how it works with the other audtool commands that don't ask for a response but require action from audacious, and to see how I can reduce scripting php files by passing a parameter to the bash script!

Metric Rat
  • 196
  • 1
  • 3
  • 12
0

Just for completeness, the php and bash scripts for both a request and an action, using a parameter fed to the php url and then on to the bash script:

PHP File with Parameter

<?php
$request = $_GET["request"];
echo shell_exec("sudo /var/www/html/cursong.sh \"${request}\" 2>&1");
?>

url example:

http://192.168.1.92/cursong.php?request="--playlist-shuffle-status" 

Bash Script with parameter

#!/bin/bash
##call request

pid=`pidof audacious`
user=`ps -p $pid -o user=`
export `strings /proc/$pid/environ | grep DBUS_SESSION_BUS_ADDRESS`
sudo -E -su $user /usr/bin/audtool $1

PHP file for an action

<?php
$action = $_GET["action"];
shell_exec('sudo /var/www/html/playsong.sh \"${request}\" ');
?>

url example:

http://192.168.1.92/cursong.php?action="--playback-play" 

Bash script for an action

#!/bin/bash
##call action

pid=`pidof audacious`
user=`ps -p $pid -o user=`
export `strings /proc/$pid/environ | grep DBUS_SESSION_BUS_ADDRESS`
sudo -E -su $user /usr/bin/audtool $1
Metric Rat
  • 196
  • 1
  • 3
  • 12