Using the `where` command through PHP - php

I am trying to get the path to certain exe's using the where command in the command prompt on windows.
Here is what i did in command prompt.
where g++
where java
where javac
where python
All of these are giving the correct output of the path in the console window which indicated that I have set the environment variables correctly.
But now When i try to run the commands using the shell_exec() function in PHP, only the call to where java and where python gives the correct output. I was even able to successfully execute a respective test file using these commands through PHP.
But strangely, where g++ and where javac give this error in the browser when run through PHP:
INFO: Could not find files for the given pattern(s).
Also if I get the outputs of these two commands on the console and then copy that into my script to compile a c++ or java file, it works perfect. But the where command returns the above INFO when run through the PHP script.
I am running the server on localhost using XAMPP. Any idea what is missing?

Related

exec('php path/to/script.php') 'php' is not recognized as an internal or external command

I have this code, as part of a php script:
exec("php runScript.php --task_id {$taskId} 2>&1 ", $scriptOutput);
Where scriptoutput returns: 'php' is not recognized as an internal or external command.
I have php in my systemvariables and when I run
php runScript.php --task_id 1
on the commandline it works without problems and i can use the php command for other things such as php composer.phar install.
But when my php script arrives at the rule with the exec code, it returns that php isnt recognized. The script works on Ubuntu, but not locally on my Windows environment. So perhaps it's a Windows issue.
I read a lot about environmentvariables but if that was the case i wasn't able to use php in the command line. Now it's only not working via exec.
I hope someone can help me with this.

PHP shell_exec() Behaves Differently Than Terminal Command Line MacOS

Scanline is a simple command line utility used for scanning documents from a twain scanner. http://blog.scottkleper.com/scanline-command-line-scanner-for-mac/
I am trying to use Scanline through a PHP script using shell_exec(); the same way that I would use it directly from the terminal in MacOS.
When I run Scanline directly from the command line, it detects all the attached scanners and prints them out ./scanline -list
When I run Scanline using shell_exec(), it does not detect any devices.
So far, I have changed the apache user to my local user, and have added the local user to the sudoers file. If I run 'whoami' in shell_exec() it is the same result as if I run it in command line.
I have printed the environment using printenv in command line, and have setup all of the same variables in my php script before executing shell_exec() using putenv(); If I run shell_exec('printenv 2>&1'), it is the exact same environment as when I run printenv in command line.
All the permissions are correct and allow access, and scanline runs when executed through shell_exec() without error (I checked apache's error logs, as well as put a error_reporting(E_ALL); in the top of the PHP file to printout any issues along the way). The only difference in how the program is executed is that in command line the devices are detected, and run through shell_exec(), no devices are found.
Any ideas as to what else I could be missing between command line and using shell_exec() ?
I also tried using system(), exec(), and shell_exec() interchangeably with the same result.

Running complicated ROS shell command through php

I'm trying to run a ROS shell program on the server through php on Ubuntu 14.04. I have tried using system, exec, shell_exec but nothing happens and I don't get any output. The system call is the following:
echo shell_exec("/opt/ros/indigo/bin/rosrun gazebo_ros spawn_model -database Part_A -gazebo -model Part_A");
What are the limitations of using system or exec to run any shell command through php on a server?
I don't care as much about the output of the command as for its execution. I think the problem has to do with the fact that PHP doesn't have any PATH like shell does so it can't find any applications without specifying the exact location. How can I make PHP se the same PATH shell does?
The problem was that the apache user and the environment in which the bash commands are running are not set up correctly. I followed the instructions from this answer but instead of using "source" I used "." and instead of using the source.bash file I used the source.sh file. I also set all the environment variables that had to do with ros or gazebo using the putenv() function.

Not able to execute rm through shell_exec in php

I have to delete various files on the server using rm.
I tried using:
shell_exec(escapeshellarg(escapeshellcmd("rm -f ./io_cache/".$prob_id."/*")))
where $prob_id is a number as well as a name of a directory. Commands like ifconfig are working fine. When using the same command with exec it returns error code 127. If I use a variant :
exec("rm ".escapeshellarg(escapeshellcmd("-f ./io_cache/".$prob_id."/*")),$out,$ret)
Then it returns error code 1. shell_exec("pwd") returns /opt/lampp/htdocs/mat and the directory io_cache is present in mat.
I also tried doing the job using a python script. When I call python script without system arguments and deleting some random files the script is running fine.But when I am using something like:
shell_exec(escapeshellarg(escapeshellcmd('./rem.py ').$prob_id))
To pass the directory from where to delete files then also nothing happens. rem.py is in mat.
I am working on Ubuntu and using apache web server

Execute a bash file located on a different VPS

I have a php webpage located on Webserver1, which is from Host1.
I also have a bash script located in Gameserver1 which is from Host2.
Is there any way to send a command from Webserver1 to Gameserver1 to execute the bash file? The webpage and file are on different VPSs. Both are running Debian 7.
The script is literally one line, to execute a java command via a screen, so the server can start if a player notices it's down. The command's available already so it doesn't need to be a secure way of hiding what the command is.
There are 2 ways I can think of. either create a bash file in Webserver1 that connects through ssh and executes the bash script you need on Gameserver1. then run it through php with exec() command.
Or you can create a php file in Gameserver1 that uses exec() to execute the bash script you need on Gameserver1 and call it using file_get_contents() on Webserver1, which is not that secure since anyone can call that file and run your script.

Categories