exec command works in terminal not with PHP - 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.

Related

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.

Executing a bash script via php

Good day all, I m trying to implement a web interface that will operate my wireless network.
One of the operations is to configure my card into monitor mode. pretty simple, if you run this command:
bash prepareCard.sh wlan0
and the script prepareCard.sh is as follows:
#! /bin/bash
IFACE=$1
ifconfig $IFACE down
iwconfig $IFACE mode monitor
ifconfig $IFACE up
Now I want to execute this script via a php script:
$cmd = shell_exec("bash prepareCard.sh wlan0");
when I check if the card has been set to monitor mode, nothing! it's still in management mode!!
Can you please tell me where did I go wrong?
Assuming the webserver user that is running the script does not have sufficient permissions, you can try this way to fix it:
Use command visudo to edit /etc/sudoers and add this line:
ALL ALL=(root) NOPASSWD: /absolute/path/prepareCard.sh
Make sure to set permissions 700 to the script, so no one else can edit it. Then execute your script with sudo like this:
$cmd = shell_exec("sudo /absolute/path/prepareCard.sh wlan0");
That should execute the script as root without a need to enter a password.
One good way to debug BASH scripts is to set debug mode on (either by passing an -x flag to bash in your shell_exec call, by running set -x, or in the shebang line #!/bin/bash -x). This shows you what's going on during execution of the bash script. In your case, I suggest the first case, since you don't know if the script is even being loaded in the first place.
$cmd = shell_exec("bash -x prepareCard.sh wlan0");
After that, you should have more in your $cmd variable.

bash script to launch freerdp with 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.

Running "Last" Linux Command

I'm trying to execute a linux command in PHP, here is my sample code:
$command = "last -F";
$o = shell_exec($command);
print_r($o);
Most of the Linux commands gives me an output, but for the Last -F command, I have no output. Why is it so?
Try This Explaination. Your issue MAY be that the last line of last -F is a new-line. shell_exec() only returns the last line of the command, and therefore, if that line is empty, you get nothing, nada.
As an alternative, try exec(), this will allow you to capture the return value (success or failure of execution) as well as the entirety of the command's output. Check it out here
You are executing that command as web user (nobody or www-data), which is a limited privileged user.You have to execute that command as root. Unfortunately giving sudo permission or full permission to web user is really a bad idea. So I recommend make a cron or background script that execute last -F and write output to a file. You can read that file from your PHP script.
You can make a script runs in background like this.
#!/bin/bash
while [ true ]; do
last -F > /tmp/myfile
done
save the code as mycron.sh
chmod +x mycron.sh
mycron.sh &
Read the file /tmp/myfile from your PHP Program. It will give the exact output of that command.

How to change the terminal working directory with cli script?

I would like to change the directory in Linux terminal from cli script, not the PHP current working directory -- hopefully with shell_exec().
Ex: from user#host:~$ to user#host:/the/other/directory$
system() and exec() are not allowed.
This isn't working in my case:
$dir = '/the/other/directory';
shell_exec('cd '.$dir);
nor these
shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
pclose(popen('cd '.$dir));
But shell_exec('ls '.$dir) gives me the list in that directory. Any trickery?
When you're executing several commands like you are doing:
shell_exec('cd '.escapeshellarg($dir));
shell_exec(escapeshellcmd('cd '.$dir));
It won't work. The first command has nothing to do with the second one, so you can't use the results of the 1st command to execute the 2nd command.
If you want to execute a chain of commands, use the pipe symbol | like:
echo shell_exec("ls /etc/apache2 | grep fileName");
Local changes made by shell_exec only apply to that php process and therefore ls would fetch from that other directory. But when you exit the php, you are back to bash who never really cares what process does.
I doubt you can change bash's current directory from outside.
If you need to run bash in other directory, you could try this inside your php:
system('gnome-terminal --working-directtory=/home/doom');
this will start another terminal in new dir but script will wait till you quit this shell.
If you mean you want to change the parent shell's directory by calling a php cli script, then you can't do that. What you could do is at best:
shell> eval `your/cli/script`
and in your script do something like
<?php
...
echo "cd $dir"
?>

Categories