Can not find Remote Shell in eclipse - android

I want to do some work for which I require shell in eclipse but I can't get it anywhere.
I have tried :
Window > Show View > other
but i didn't get any option over there.
Shall I download something for the shell, or do any settings to get the shell ?
Give any link.
Thank you.

You need Eclipse RSE (Remote System Explorer) installed for this support.
Go to Help > Install New Software, choose your Eclipse release to work with. RSE is in the Mobile and Device Development section.

You can also access using java code
Process p;
p = Runtime.getRuntime().exec("//enter Your shell Command here");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
sb.append(line);
while (line != null) {
line = reader.readLine();
sb.append(line);
}
System.out.println(sb.toString());//ur shell command result

Related

Can't run exec("su") on a rooted Android Marshmallow device, other commands run fine

I have gone over all the possible answers to this questions, and none of them has worked for me. Seems like Android has very tightly blocked the running of the shell commands from within an app, even when the device is rooted.
I can do su and run commands perfectly fine from adb, but I cannot run them from the app itself. It fails with error:
Error running exec(). Command: [su] Working Directory: null Environment: null
I have searched for these errors and the solutions work for older versions of Android, not Marshmallow.
I have also tried libsuperuser (SuperSu), but it also fails on Marshmallow.
Other things I tried were like, e.g. for the device reboot:
Process p = Runtime.getRuntime().exec("su");
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
or
PowerManager pm = (PowerManager)...
Same with other operations. The commands fail right at su. I can't issue an adb command either from the app. But I can do both perfectly fine otherwise from the command line via adb shell.
The example which Android has on its website doesn't work either.
I read about disabling SELinux security and setting it to permissive mode. But that also require to run su first, where the app fails.
Now I am totally lost and don't know what to do next. I am really looking for a working solution since for this particular project, which requires the app to interact with the underlying hardware.
You need to first ensure you have busybox installed as that would install the list of most commonly used shell commands and then use the following code to run the command.
Runtime.getRuntime().exec("ls");
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(nfiq.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
chmod.waitFor();
outputString = output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Try this
Process command = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes("-c reboot\n");
dos.writeBytes("exit\n");
dos.flush();
dos.close();
p.waitFor();
It can take time to execute so u might wanna run this on worker thread
There is nothing that blocks shell commands in "Marshmallow" . I am using it inside my app and all commands work flawlessly. There is something else you are missing. First confirm that in your "/system/bin/" directory all files are present. Alternatively check in other device.
Try to add "sh -c " as the beginning of your shell command. Otherwise you will get the error message about null directory and environment.
Example: sh -c su

Trigger UIAutomator Test Script programmatically

We are using UIAutomator for automation.
To run UIAutomator test scripts we used to compile the scripts from command prompt by using either USB or Wifi adb and it will run on the android device.
UiAutomator scripts will come in jar format after compilation.
We will push the jar in the device and we have trigger the test scripts.
The command to start the test script in “adb shell uiautomator runtest TestPackage.jar”.
It is working with normal adb connection.
But in our case we have to initiate the test scripts without any adb connection.
So we tried by passing the commands programmatically in the device using this java code
try {
StringBuffer output = new StringBuffer();
Process p = Runtime.getRuntime().exec(params[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
p.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
After the execution we are getting the following exception:
java.lang.securityException : longMsg = You do not have android.permission.RETRIEVE_WINDOW_CONTENT required to call registerUITestAutomationService
If we run scripts from command prompt with adb connection it is working. But if we tried the command programmatically we are getting this exception.
We came to know that the above mentioned issue can be overcome in a rooted android device by enabling the RETRIEVE_WINDOW_CONTENT permission. But we want this scripts to run on a non-rooted setup.
Can anyone help me in explaining this security permission issue?
How can I resolve it?

Android get GPU model

When running the following command from termial:
adb shell dumpsys | grep GLES
The output is:
GLES: Qualcomm, Adreno (TM) 330, OpenGL ES 3.0 V#53.0 AU# (CL#)
However I am unable to get the output when running programatically.
String GPUModel = "";
String command = "adb shell dumpsys | grep GLES";
try {
InputStream inputStream = Runtime.getRuntime()
.exec(command)
.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
GPUModel = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
GPUModel is null.
You can't run the dumpsys command from your app. It'd require the DUMP permission which only system apps and apps signed with the same key as the system are granted.
You should use glGetString to get GPU type:
String renderer = GLES20.glGetString(GLES20.GL_RENDERER);
However, if you need to check for certain feature of GPU you better not to check GPU name but check if necessary GL extension is available instead. You can retrieve all of them by requesting GL_EXTENSIONS:
String extensions = GLES20.glGetString(GLES20.GL_EXTENSIONS);

Android logcat command

I want to get "last 25 logs of tag name CordovaLog"
I tried
logcat -t 25 CordovaLog
I do get 25 logs but of all the tag names. Also what if there are no 25 Cordova Log ? Will it return whatever is available.
So what is the right command ?
Thanks.
EDIT 1-
Process process = Runtime.getRuntime().exec("logcat -d -t 25 CordovaLog:V *:S");
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
{
log.append(line+"<br />");
}
return log.toString();
So Is this code getting all the logs are filting to get "Cordova logs" ? OR it's just getting "Cordova logs" ?
It's crashing as I have lots of logs.
the correct use will be
logcat -d -t 25 CordovaLog:V *:S
With this, you will print all that have the tag CordovaLog, and will silent everything else.
You can have more information in the official documentation.
Edite to fix the correct answer (see comment below). It will print the last 25 entries, or less in case there are less than 25 entries.

How can I run Linux commands on an Android device?

On some Android devices, in the ADB shell, I can only run echo, cd, ls. When I run:
tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk
Or the command cp, it returns:
sh: tar: not found
Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/data folder to SD card. I got su and I got the following code:
int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
+ packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
process.getInputStream())), 64);
String inLine;
try {
StringBuilder sbCommand = new StringBuilder();
sbCommand.append(command).append(" ");
sbCommand.append("\n");
os.writeBytes(command.toString());
if (is != null) {
for (int i = 0; i < timeout; i++) {
if (is.ready())
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (is.ready()) {
inLine = is.readLine();
} else {
}
}
} catch (IOException e) {
e.printStackTrace();
}
It always stops in is.ready(), and when I changed it to process.waitfor() it also stopped. Why?
As far as i know, the only way to run shell commands is:
Process proc = Runtime.getRuntime().exec("your command");
You can run Linux commands on Android. But there are usually just very few pre-installed.
If you want to add more commands you might want to root your device and install busybox on it.
This is not for productive use within an application but can help you to work with your device.
If you have the binaries for your system, you can run anything on your system.
Saying that you have to understand that you have to find the binaries for tar.
Look here http://forum.xda-developers.com/showthread.php?t=872438
And possibly other places..
You can probably get this done by using a Terminal Emulator app. As you wrote above, I don't know how well DOS commands will work. But, a Terminal Emulator works without root.
You can install Termux app on your android device and run Linux command by using that app
Install busybox, then type the command in the following format:
busybox [linux command]
You cannot use all the linux commands without busybox, because Android doesn't have all the binaries that are available in a standard linux operating system.
FYI, a binary is just a file that contains compiled code. A lot of the default binaries are stored in /system/bin/sh directory. All these commands like 'cp' 'ls' 'get' etc, are actually binaries. You can view them through:
ls -a /system/bin/sh
Hope this helps.
In reply to Igor Ganapolsky, You would have to have a database set up for locate.
Probably find would be adequate for your needs.
example:
find -name *.apk

Categories

Resources