exec in php is working inappropriately - php

I am willing to execute a shill command through PHP but i faced that the command is not executing , here is the command:
exec('/cutycapt/CutyCapt --url="' . $source . '" --out="/home/user/NetBeansProjects/PhpProject1/htmlImage/example.png"');
i tried as testing to execute the following :
echo exec(' ls /cutycapt/');//print_r is the same
only one file returned while this command returned them all
echo system(' ls /cutycapt/');
i tried to use the "system" method instead of exec in the first command and the result was the same
what could affect the command so it wan't execute ?
update
the case i'm talking bout the the first command work whether i run it in the terminal or i run the PHP script in terminal too but when i run it from the browser (the php script )it doesn't work !!

Look into manual - http://de3.php.net/manual/en/function.exec.php
exec and system returns "The last line from the result of the command"

In case of system and exec the last line from the result of the command gets returned.
If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

I had the same problem working with external commands in php. The problem was related to file permissions. I was using "vchiq" library and the error was "* failed to open vchiq instance". This page might work for you.

Related

PHP execute SSH2 command

I'm having some problems with PHP SSH2 commands.
So I have succesfully connected my dedicated server with my app, but I have no idea how to run command with that.
I'm using phpsclib ( http://phpseclib.sourceforge.net ), when I write something like
echo $ssh->read('username#username:~$');
$ssh->write("ls -la\n");
echo $ssh->read();
It will give me a list from root, but the problem is I don't want to be in root.
I need to cd /another_file and execute command that will start some game server.
I tried something like
$ssh->write("cd /another_file");
$ssh->write("my command here");
But no success, it's showing commands in same line.
Any ideas how to do that?
You have several options.
Chain the commands using $ssh->write():
$ssh->write("cd /another_file && ls -la\n");
...or...
$ssh->write("cd /another_file; ls -la\n");
Chain the commands using $ssh->exec(). Works much the same way as above except that you don't need to append \n to your command and you don't need to bother with the $ssh->read().
Do multiple $ssh->write() calls, which is what your current attempt is attempting to do. But it's doing it wrong:
$ssh->write("cd /another_file");
$ssh->write("my command here");
In your first code snippet you're appending a \n but you're not doing so on these lines. You need to because, when you're in an interactive shell, the way the shell knows that you're done typing the command out is because you hit the enter button. That's how Linux knows you're done typing and are ready for your command to run.
You also need to read the prompt before running the next command. eg. $ssh->read('[prompt]') or whatever is appropriate for your system. I suppose you could just omit passing a parameter to $ssh->read() all together, as you do in your first code snippet, but then it'll take 10s or so for $ssh->read() to return any output when it could have alternatively been returning output instantly.

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.

how to execute a source command from php in a server

I am unable to execute a source command in linux using php.All other commands are working except this one. I need to execute the following command.
source /root/Envs/ate/bin/activate
This activates the ate-Automatic Test Equipment.Once I activate it then I need to run a python script as the script accesses the remote server.
I am able to manually run it but I am creating a tool which will automatically do it.
<?php
exec("source /root/Envs/ate/bin/activate", $output, $return);
echo "Command returned $return, and output:\n";
echo exec("python box_upgrade-pradeepa.py");
?>
The above commands returns 1 which means there is an error.But I am not sure how to run the 'source command'. The python script will run only if the source command is successful.(the python command is correct as I replaced hello.py and it ran fine.)
Could you pls help me as I am really stuck for a week?
Thanks a lot..
I found out the error. Since I am doing it using php (for a web tool) the user is Apache. 'Apache' user is unable to access the script in root folder. Moving it to another directory, I am able to run the script fine.
Thanks all..

Echo exec not working tesseract

I'm working on using tesseract with PHP and this is my first time using such things as exec.
/usr/local/bin/tesseract /images/hello.png stdout works perfectly via SSH but nothing happens when I try and run this via PHP;
echo exec('/usr/local/bin/tesseract images/hello.png result');
But if I try;
echo exec('/usr/local/bin/tesseract images/hello.png result 2>&1');
Then the page writes Tesseract Open Source OCR Engine v3.03 with Leptonica So I feel like I must close.
Any ideas?
Got it working using the following
exec('/usr/local/bin/tesseract /images/hello.png stdout', $msg);
print_r($msg);
http://php.net/manual/en/function.exec.php
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.

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