Adb shell commands are not working in php - php

adb shell commands are not working in php but they are working in command prompt.
In window 7, I tried to execute 'adb devices' command via php as below. I also tried with shell_exec() and system(). But none of them are working. I ran 'ipconfig' command in php and it's working fine. It's just 'adb' that's not working.
<?php
exec('adb devices', $output);
echo '<pre>'.$output.'</pre>';
?>
I want it to return me the list of devices attached to the pc.

the issue you have is connected to the PATH variable. This variable contains all paths where the shell tries to look for executables.
Your personal terminal should have configured the PATH variable to contain the absolute path to Android tools. Unfortunately, it's not shared with PHP runtime.
One of the solutions can be to use absolute executable path in your code:
exec('C:\any_path\Android-SDK\bin\adb devices', $output);
You can either make the PATH variable used by PHP process to contain the absolute path to Android tools.

Related

Running gcloud command in php not working

I am trying to run shell commands from PHP using
<?PHP
$output = shell_exec('gcloud --version'." 2>&1");
echo "$output";
?>
The output which I get is
sh: 1: gcloud: not found
When I try to do
shell_exec('ls -l')
It works as expected. I have related posts on StackOverflow to use Rest API instead on this. But I have created a big script with gcloud commands. I am running my shell scripts in Terminal in mac. Can anyone help me understand the issue. May be required to install sdk, tried that still got same issue.
You need to read up on Relative Path vs Absolute Path of a file/dir.
When you code shell_exec('gcloud --version'..., you are using relative path.
One way to resolve your issue is to specify the absolute path to gcloud command which can be determined from bash command line by typing:
which gcloud and you might get a result like
/usr/lib/google-cloud-sdk/bin/gcloud
Then, change your PHP code to shell_exec('/usr/lib/google-cloud-sdk/bin/gcloud --version'...

Generate android apk using shell script from php?

I'm trying to generate Android apk files from shell script, I want to execute a shell script file from PHP. When I run the shell script in the terminal, it works perfectly. If I try to run the script using PHP, the shell script doesn't execute all the commands. The ls command in shell script works perfectly, but when executing using PHP, other commands doesn't work. I'm using xampp server in a Linux environment.
My shell script
cd /home/user/AndroidStudioProjects/msvep4247-inghamautogroup-pulse-and/
./gradlew assembleDebug
cp -fr app/build/outputs/apk/app-debug.apk /opt/lampp/htdocs/sample/apk
ls
Shell script ls output
app autolead_data_format.pdf build build.gradle cheek gradle
gradle.properties gradlew gradlew.bat lib local.properties
msvep4247-inghamautogroup-pulse-and.iml settings.gradle
My PHP script
<?php
echo shell_exec('ls');
echo shell_exec('./generateApk.sh');
?>
PHP script ls output
generateApk.sh generate.php APK
Note: ls outputs file names in the folder
I set all the file permissions for shell script in the xampp server. Can anyone describe where I'm mistaken? Awaiting responses...
Just use the full path to the script/executable, because the environment is different when running from php.
It seems that PATH environment variable in PHP code that is executed in the web server is more limited than the one in the shell you're working in. But you can change environment variables in PHP, and the commands you start from it will see those changes.
<?php
// set content type so the output is more readable in the browser
header('Content-Type: text/plain');
// set $PATH to some limited value
putenv('PATH=/bin:/sbin');
// verify, note that we have to use full path to 'env'
print(shell_exec("/usr/bin/env|grep '^PATH='"));
// this command won't run (assuming its full path is /usr/bin/id)
print(shell_exec("id"));
// add more directories to $PATH
putenv('PATH=/bin:/sbin:/usr/bin:/usr/sbin');
// verify again, we can use env without specifying the path this time
print(shell_exec("env|grep '^PATH='"));
// this command will
print(shell_exec("id"));
So you have to write putenv('PATH=<your_shell_PATH_contents>'); at the top of your PHP script. Using full path to the shell script alone won't help if the script itself uses relative paths to binaries it starts.

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.

Using the `where` command through 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?

Can't get shell_exec to download a file

I have a multithreaded cli downloader for ubuntu called Aria2c.
I've been trying to get a php script to run aria2c and download a file using shell_exec, but i can't seem to get it to work. Ultimately I plan to have an input box on a page where I can enter a link and aria would download it.
Here's the code I've come up with (for now im inputting the link manually):
<?php $dl = shell_exec('aria2c -d /home/user/ www.downloadlink.com'); ?>
Note that the aria2c command I specified works well in the shell; and the directory I'm attempting to download to is set to '777'.
I'm baffled as to why it's not working, any ideas?
PS: I prefer to use aria rather than the alternatives because it is multithreaded and it supports cookies.
Check if PHP is running in safe_mode. shell_exec won't work if safe_mode is on.
EDIT: aria2c was not referenced with a full path. Referencing it like this: shell_exec('/path/to/aria2c -d /home/user/ www.downloadlink.com') works.
I'll make the assumption that you are running PHP through a web server. In such case, it's very unlikely that the web server has permission to write into your user's home directory: Apache runs as daemon with the credentials of a limited user. Also, the PATH env variable in Apache is not necessarily the same as your user's PATH. Last but not least, you don't check the return value or the script output.
Try something like this:
<?php
exec('/path/to/aria2c -d /tmp www.downloadlink.com', $output, $return_var);
var_dump($output, $return_var);
?>
You can get the full path for aria2c with:
which aria2c

Categories