bash script to launch freerdp with php - php

I will launch a bash scrip for php.
the echo of my bash displays the correct code syntax but does not launch ...
#!bin/bash
var1=$1
var2=$2
config=$("xfreerdp /v:server.domain.tld /u:$1 /p:$2 /load-balance-info:\"tsv://ms terminal services plugin.1.programmes_remot\")
eval "$config"
I turn your senses after my php code
system("/var/www/test/./script.sh $var1 $var2");
I just tested the method 2 no error but its not launch xfreerdp this is what I put
$cmd='xfreerdp /v:server.domain.tld /u:'.$var1.' /p:'.$var2.' /f /cert-igore -menu-anims /network:lan load-balance-info:"tsv://MS Terminal Services Plugin1.Programmes_Remot"';
exec('export display=guilinuxbox:0.0 $cmd');
but this does not launch

A slash is missing in the she-bang: #!/bin/bash
Anyway, your script seems rather complicated. You could just write:
#!/bin/bash
xfreerdp /v:server.domain.tld /u:$1 /p:$2 /load-balance-info:\"tsv://ms terminal services plugin.1.programmes_remot\"
or call xfreedb directly in your PHP script:
$var1 = escapeshellarg($var1);
$var2 = escapeshellarg($var2);
system("xfreerdp /v:server.domain.tld /u:$var1 /p:$var2 /load-balance-info:\"tsv://ms terminal services plugin.1.programmes_remot\"");
Note, that you should always escape the variables that you put in a shell command for security reasons.
Edit:
Ah okay, it’s a GUI/X11 application. Are you running the PHP script on a Web server? Add 2>&1 to the command line string like this system("... 2>&1") to see any error messages in the HTML output.
I guess, you also need to explicitly grant access to the screen.

Related

PHP/Ubuntu - QxcbConnection: Could not connect to display aborted

I am using a php script on my apache/ubuntu server to call a bash script that triggers an application taking a python script as an argument (IDAPro).
PHP Code
chdir('/var/www/dashboard/team/static/sql');
$output = exec('sudo -u rohan ./start.sh');
Now, the above code works fine if I run the PHP file from the terminal - but only if I run it as the root user. Needless to say, if I execute the bash file directly it runs too.
But when I run the PHP file on the browser, it doesn't work and I get the following error in the apache error log:
QXcbConnection: Could not connect to display
Aborted
I understand that Apache/php runs as 'www-data' user (used the 'whoami' to verify), and that is why I have the sudo in my exec. I have tweaked and tinkered the permissions for both users to no avail. When I run the php file from the terminal as the 'www-data' user, it throws no error but does not do anything except display the random echo tags I at the start and end of the script to debug it.
I am a linux novice, so any help is greatly appreciated.
Okay, I finally managed to solve it.
The issue is not with the permissions, but it is with the environment variables.
I had to include the following line in my bash script
export DISPLAY=':0.0'
Note that setting the variable in the terminal and running the script does not work. The line needs to be inside the script.
I assume this is because the DISPLAY variable is not set if you run the script as any user other than root, which is what happens in case of Apache/PHP where the script is executed as the 'www-data' user.
perhaps you could use something like the following at the top of your script:
if [ "$(id -un)" != "rohan" ]; then
exec sudo -u rohan $0 "$#"
fi
export XAUTHORITY=/home/rohan/.Xauthority
export DISPLAY=:0

Shell script command "ldap_search" is not working with php exec or shell_exec command

I'm developing a code which uses ldap_search Shell Script Command for extracting user information from Active Directory using user id and by proper LDAP Server Authentication. I am getting accurate result from ldap_search script.
But, whenever I put the shell script inside exec or shell_exec PHP command, I'm not getting anything.
All the other shell scripts are working fine with the help of PHP exec command except ldap_search.
Is there some additional task left for me to do?
Is ldap_search and exec/shell_exec not compatible with each other?
You must use echo exec('your command or script');
Make sure to have permissions to run it. I mean, the web user must have permissions to execute that.
May seem obvious, but I think your failure is in something basic like this. You must put echo to show the result of the command.
EDIT After reading your new comments about it and using that new info... I saw you are trying to redirect the output to a file... but maybe you have 2 different problems.
Have the user which is executing php (usually www-data) permission to write on the folder where the php is?
Your code has quotes inside quotes that must be escaped using . Try this:
<?php exec("ldapsearch -x -v -h 'LDAP://server' -p '389' -D 'uid=\"domain_user_id\",ou=users,ou=internal,o=\"organization\"' -w 'domain_password' -b 'ou=users,ou=internal,o=organization' 'uid=person's_user_id' >> result.txt"); ?>
So you don't need echo if you want the output in a file. And the redirection >> can be inside the command executed, not in php.
Remember that > replaces de file and what you have >> add at the end of the file.

PHP Run bash script from php file acting strange

I've written a bash script that launches a browser on an xServer and takes a screenshot of the browsers.
If I run it with the apache2 user (www-data) its working flawless, even when i use the php interactive shell and run it via shell_exec or exec its working perfectly.
However when I run it from my php file via browser it does not seem to work properly.
The script does not seems to run the xterm command (for launching the browser) and takes no screenshots, it only executes sleep and kill commands. I spent the whole day looking for a solution or at least a proper way to debug but i cant seem to fid anything
Turn on errors to start with
error_reporting(E_ALL);
ini_set('display_errors', '1');
Then change your command to
shell_exec('bash /home/daemon/daemon.sh');
The last part of your command pipes all output (including errors) to /dev/null - E.g to no where so you will not see any errors at all.
Do this and re-run the command and you should see errors.

export shell environment variable before running command from PHP CLI script

I have a script that uses passthru() to run a command. I need to set some shell environment variables before running this command, otherwise it will fail to find it's libraries.
I've tried the following:
putenv("LD_LIBRARY_PATH=/path/to/lib");
passthru($cmd);
Using putenv() doesn't appear to propagate to the command I'm running. It fails saying it can't find it libraries. When I run export LD_LIBRARY_PATH=/path/to/lib in bash, it works fine.
I also tried the following (in vain):
exec("export LD_LIBRARY_PATH=/path/to/lib");
passthru($cmd);
How can I set a shell variable from PHP, that propagates to child processes of my PHP script?
Am I limited to checking if a variable does not exist in the current environment and asking the user to manually set it?
I'm not 100% familiar with how PHP's exec works, but have you tried: exec("LD_LIBRARY_PATH=/path/to/lib $cmd")
I know that this works in most shells but I'm not sure how PHP doing things.
EDIT: Assuming this is working, to deal with multiple variables just separate them by a space:
exec("VAR1=val1 VAR2=val2 LD_LIBRARY_PATH=/path/to/lib $cmd")
You could just prepend your variable assignments to $cmd.
[ghoti#pc ~]$ cat doit.php
<?php
$cmd='echo "output=$FOO/$BAR"';
$cmd="FOO=bar;BAR=baz;" . $cmd;
print ">> $cmd\n";
passthru($cmd);
[ghoti#pc ~]$ php doit.php
>> FOO=bar;BAR=baz;echo "output=$FOO/$BAR"
output=bar/baz
[ghoti#pc ~]$
A couple things come to mind. One is for $cmd to be a script that includes the environment variable setup before running the actual program.
Another thought is this: I know you can define a variable and run a program on the same line, such as:
DISPLAY=host:0.0 xclock
but I don't know if that work in the context of passthru
https://help.ubuntu.com/community/EnvironmentVariables#Bash.27s_quick_assignment_and_inheritance_trick

exec command works in terminal not with PHP

I'm writing a class who let me access to recutils through PHP.
I have a 'database' file called books.rec in ../database/ and a script who runs my Recutils.php class.
My class simply launch system application with correct parameters.
But When I try to use recins with PHP's exec function, the command doesn't work will it work in command line.
This is the command that is executed by my script :
recins -f Title -v "Moi" -f Author -v "Moche" -f Location -v "loaned" -t Books ../database/books.rec
With PHP : Nothing, the record is not inserted (no error message at all too).
In terminal : OK, the command is well done and my record is inserted.
I also have a method to do a select operation using recsel and it works very well, will it use exactly the same file (and runs from exec too).
So, could someone explain me why the command don't work will another with the same file work ?
Thanks
PS : Further informations : http://www.gnu.org/software/recutils/
I would double check that you are running the command as the same user from the command line and your php script. That may be the problem. exec('whoami')
You said you had a script that starts your php script it should be the same user as that.
You might also want to running a simpler exec command to see if that will work first.
Other things to try:
Try checking stderr output exec('ls /tmp 2>&1', $out); This will redirect standard error to standard out so you get both.
Try using php's shell_exec() which will invoke a shell just like when you are running from the command line(eg. bash). shell_exec('ls /tmp 2>&1 >> /tmp/log') should even put all output into a log file.
I don't think this will help you but it is something to try if all else fails, set it as a background process and see if it completes. exec('nohup php process.php > process.out 2> process.err < /dev/null &'). The & will set the command to run in the background and let the script continue.
Good Luck
Is recins command accessible for PHP ? Also is path to books.rec correct ?
Try with absolute path.

Categories