I want to set two commands in one process on android.
I know that nativeProcess = Runtime.getRuntime().exec("su"); but I want to add whoami in the same process. This is possible?
try this one:
public static void go()
{
String[] command = {"/system/bin/sh", "-c", "ps | grep apps ; ls"};
ProcessBuilder processBuilder = new ProcessBuilder(command);
try {
Process process =processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",
Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
Log.e("output of go-->", "==>"+line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
su -c "command" will run your command as superuser (something like sudo in Debian based systems)
Related
I have connected three cameras to my phone and i want to know their ip addresses. In windows and linux its with the arp command but this command does not run on the android terminal . How can i run arp command on android or it there any other way to achieve the same?
use this code work for me.
try {
Process process = Runtime.getRuntime().exec("cat /proc/net/arp");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
String output = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Log.d("*************", ""+output);
}
catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
return "";
i want to get logcat data programmatically i used the below code
StringBuilder debuglog=new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debuglog.append(line);
debuglog.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Above code is working fine
This line "logcat -d" shows -d,-w,-i messages
But,my problem is i want to show -e messages
To get only ERROR use this:
Process process = Runtime.getRuntime().exec("logcat -d *:E");
All I want to achieve is to get the response from the execution.
For example if I execute command like this "ls"
I want to get string with all the files and directories
For example like this
Runtime.getRuntime().exec("ls");
But I do not know how to get the response.
I run something like this, I thought that I will redirect the output but still nothing
Runtime.getRuntime().exec("ls",null,new File("/sdcard/myFile") );
I tried also something like this but still nothing
Runtime.getRuntime().exec("echo | ls > /sdcard/myfile");
Any ideas ?
Have you tried this?
String cmd = "commands";
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()).length() > -1)
{
log.append(line);
}
try this code
try {
String[] commands = {"dumpstate > /sdcard/log1.txt"};
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
I am trying to get the output of android shell command 'getprop' with java since getprop() always returns null no matter what.
I tried this from developer.android.com:
Process process = null;
try {
process = new ProcessBuilder()
.command("/system/bin/getprop", "build.version")
.redirectErrorStream(true)
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = process.getInputStream();
//String prop = in.toString();
System.out.println(in);
process.destroy();
However what is printed is not the output but a bunch of characters and numbers (dont have the exact output right now).
How can i get the output of the process?
Thanks!
Is there any particular reason why you want to run the command as an external process?
There is a simpler way:
String android_rel_version = android.os.Build.VERSION.RELEASE;
However, if you really want to do it via a shell command, here is the way I got it to work:
try {
// Run the command
Process process = Runtime.getRuntime().exec("getprop");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Grab the results
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line + "\n");
}
// Update the view
TextView tv = (TextView)findViewById(R.id.my_text_view);
tv.setText(log.toString());
} catch (IOException e) {
}
firstly I will present my situation.
I need to execute "su" command in my android app and it works well. Then I need to execute "ls" command and read the output. I'm doing it by getting the output stream from the "su" process and writing my command into it.
And here goes the question. How to read the output of the "ls" process? All I have is the "su" Process object. Getting the input stream from it gives nothing, because "su" doesn't write anything. But "ls" does and I don't know how to access its output messages.
I have searched many sites but I didn't find any solution. Maybe someone will help me:)
Regards
Ok, I've found a solution. It should look like this:
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("ls /data\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
//do something with the output
Hope it will be helpful for someone
public String ls () {
Class<?> execClass = Class.forName("android.os.Exec");
Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
int[] pid = new int[1];
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
String output = "";
try {
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
}
catch (IOException e) {}
return output;
}
Check this code mentioned here:
How to run terminal command in Android application?
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.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();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
References
this code
GScript
I modified accepted answer by #glodos for following problems:
the streams are closed, otherwise the exec process hangs forever, on the opened stream. If you execute ps in shell (ie adb shell)
after several executions then you'll see several su processes
alive. They needs to be properly terminated.
added waitFor() to make sure the process is terminated.
Added handling for read=-1, now commands with empty stdout can be executed. Previously they crashed on new String(buffer, 0, read)
Using StringBuffer for more efficient strings handling.
private String execCommand(String cmd) throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdout = new DataOutputStream(p.getOutputStream());
stdout.writeBytes(cmd);
stdout.writeByte('\n');
stdout.flush();
stdout.close();
BufferedReader stdin = new BufferedReader(new InputStreamReader(p.getInputStream()));
char[] buffer = new char[1024];
int read;
StringBuffer out = new StringBuffer();
while((read = stdin.read(buffer)) > 0) {
out.append(buffer, 0, read);
}
stdin.close();
p.waitFor();
return out.toString();
}
Some credits go to #Sherif elKhatib ))