application that execute command in /system - android

How to create an application that runs a command in /system.
The command that I need to give is: udpxy -p 4022.
This command will start a program that I have in /system.
I do not know which commands use in creating application, which execute this command.
If anyone can help I'll be very grateful.

try {
Process process = Runtime.getRuntime().exec("/system/udpxy -p 4022");
process.waitFor();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
http://developer.android.com/reference/android/os/Process.html

Related

How to run .sh file from android app?

My device has been rooted and now i want to run an .sh file from my android application. I tried with following code but it did't provide the intended output:
Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
If i run .sh file from adb it is working fine for me.
Try following code.
try{
Process root = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(root.getOutputStream());
os.writeBytes("sh /system/bin/xyz.sh \n");
os.flush();
}
catch (IOException e) {
e.printStackTrace();
}
catch (SecurityException se){
se.printStackTrace();
}
This snippet worked for me,I hope this may help you.
Process process = Runtime.getRuntime().exec("sh /data/local/tmp/xyz.sh");
Scanner stdout = new Scanner(process.getInputStream());
while (stdout.hasNextLine()) {
Log.i("stdout", stdout.nextLine());
}
stdout.close();
Scanner stderr = new Scanner(process.getErrorStream());
while (stderr.hasNextLine()) {
Log.e("stderr", stderr.nextLine());
}
stderr.close();

Modifying other app's database with root in Android

How can I read and modify the database of an app that I don't own with root.
I tried copying the file to my app's location like this:
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("su -c cat /data/user/0/com.maxmpz.audioplayer/databases/folders.db > /data/user/0/com.example.me.myapp/folders.db");
} catch (IOException e) {
e.printStackTrace();
}
and:
try {
Runtime.getRuntime().exec("su");
Process p = Runtime.getRuntime().exec("su cp /data/user/0/com.maxmpz.audioplayer/databases/folders.db /data/user/0/com.example.me.myapp/folders.db");
} catch (IOException e) {
e.printStackTrace();
}
But nothing happens (the exception isn't raised).

Android shell commands fail to find /data directory

In my application I'm doing:
try {
String[] cmd = {"su", "-c", "\"ls /data/\""}; //to debug, will be cp /src /dest
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
Log.e("copy", is.toString());
Log.e("copy", convertStreamToString(is));
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
The app is installed in /system/app/ and running with root permissions.
I see SuperSu's overlay that it's granted permissions for the operation.
With the cp /src /dest in place of ls command above, it doesn't copy, so debugging with ls, I get:
tmp-mksh: ls /data: not found
Why is this, and how can I fix it?
NB: This is the same issue as this question, except that was resolved by adding external write permissions - I should note that both paths in my command are in /data/...

How to Shutdown Android 4.0 rooted Device programatically

Please help me to find out solution for this, I Know there are so many questions and duplicates about this same but here i describe whole things which i tried.
I have one android device where its installed 4.0 version of android.
I want to shutdown this device using my one demo application.
1) Demo application is signed by platform keys which are used in built in file system.
2) Device is already rooted as its development board and i have all permissions on this.
Application contains Following things
1) Application is system application
2) Application signed by platform keys which are used in built in file system.
For make automation easier, I did import the key/cert pair into my java keystore file, with the this keytool-importkeypair and use eclipse for signing.
Used command is mentioned below.
Commad : keytool-importkeypair -k ~/Desktop/myown.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform
I used following code for reboot but i never make success in this .I read So many questions and answers on stackoverflow but they all said you require
1) root access of device
2) signed apk with any one keys which are available on `build/target/product/security/`
3) Given Proper permission in AndroidManifest.xml file.
Am i right in alomg points?
Application code :
First Try
public static void shutdown2() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
String command = "/system/bin/reboot -p";
try { // Run Script
proc = runtime.exec("/system/xbin/su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (proc != null)
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (proc.exitValue() != 0) {
}
}
Second Try :
private void shutdown3() {
try {
String[] cmd = { "/system/bin/sh","su","reboot -p"};
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
} catch (Exception ex) {
Log.i("TAG", "Could not reboot 3 ", ex);
}
}
3rd Try :
private void shutdown() {
try {
Process proc = Runtime.getRuntime().exec(
new String[] { "/system/bin/su", "-c",
"/system/bin/reboot -p" });
proc.waitFor();
} catch (Exception ex) {
Log.i("TAG", "Could not reboot 1 ", ex);
}
}
In 3rd method I also tried with "/system/bin/su"
The below code worked for me
Process process = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "busybox poweroff -f"});
process.waitFor();
A much better solution is to run:
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
You can use a Process for this.

Run android program as root

Is it possible to run my program as root? I know how to run command-line native utils, but how to run Java program as root?
This will work for you:
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
You could use this for a command:
exec(new String[] { "su", "-c", COMMAND });
Best wishes,
Tim

Categories

Resources