Run su -c "command" process - Java for Android App Development

I want to use su to launch a command and read its output.
I try this code:
Code:
String line;
Process cat=Runtime.getRuntime().exec("su -c \"cat /etc/media_codecs.xml\"");
BufferedReader catStream= new BufferedReader(new InputStreamReader(cat.getInputStream()));
BufferedReader catSerr= new BufferedReader(new InputStreamReader(cat.getErrorStream()));
cat.waitFor();
while((line=catSerr.readLine())!=null)
{
System.out.println(line);
}
while((line=catStream.readLine())!=null)
{
System.out.println(line);
}
If I manually insert this command in the adb shell it works but with this code the phone ask me for root permission and If I accept I can read only this string to the stderr:
tmp-mksh: cat /etc/media_codecs.xml: not found
And there is not stdout code.
Why?
Thanks,
regards
A993

Related

Android SDK Emulator - Root Access

When I run the android emulator, using the Android 2.2 - API Level 8, by issuing from cmd emulator -avd test (test being the emulator name) I am not given root access. If I open terminal emulator and issue the SU command i recieve permission denied.
I thought the emulator was meant to run in root access?
I wish someone else would shed some light on this as well. I feel like I have been able to gain access to root, using the su command, but I cant remember as it has been a few months since using the emulator.
I also help someone else will look into this...
Sent from my DROIDX using XDA App
ANy update on this topic?
Hey guys, could anyone fill me in on how to get root in the emulator?
Does anyone knows the answer for this?
I tried chaining the commands, but still not working
Code:
try {
Runtime rt = Runtime.getRuntime();
String[] cmd = { "su", "-c", "ls -l /data/data/"};
Process pcs = rt.exec(cmd); //("ls -l /data/data/");
BufferedReader br = new BufferedReader(new InputStreamReader(pcs
.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
Log.e("line","line="+line);
}
br.close();
pcs.waitFor();
int ret = pcs.exitValue();
Log.e("ret","ret="+ret);
} catch (Exception e) {
Log.e("Exception", "Exception", e);
}

Need help with wget implantatet in App

Hi,
I have a wget command that I want to implantate in an app. the command looks like this
It's executed in a terminal:
Code:
wget --post-data='loginwe=YOURLOGIN&passwordwe=YOURPASS' \
> --save-cookies=my-cookies.txt --keep-session-cookies \
> https://HOSTNAME?cmd=ACTION
the page information have to be saved in a string that will be searched for information.
Can someone help me with the code that can do this job?
UPDATE!
This is what I have tried to incorporate in my app. but I do not get logged in.
Code:
public String getSaldo(String number, String passy) {
HttpClient client = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("https://HOSTNAME?cmd=ACTION");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("phoneNumber", number));
params.add(new BasicNameValuePair("password", passy));
try {
postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(postRequest, responseHandler);
Is there someone who can see why the httpClient command isn't working?
Why not instead try to leverage org.apache.http.client classes and avoid the messiness of using a shell?
http://developer.android.com/reference/org/apache/http/package-summary.html
If you want to go the shell route, check out:
java.lang.Runtime.getRuntime().exec();
it's fairly easy to see what you're getting at but I just thought I'd mention that implantatet is most definitely not a word. not in english anyway..
Thanks Info Updatet

Terminal command for reboot in recovery

Hi,
what is the command to reboot in recovery bootloader ecc?
I will insert in my app through this library
rampo said:
Hi,
what is the command to reboot in recovery bootloader ecc?
I will insert in my app through this library
Click to expand...
Click to collapse
The command for rebooting is
Code:
reboot
Use that to reboot into recovery:
Code:
reboot recovery
For bootloader you need to use adb:
Code:
adb reboot-bootloader
Runtime.getRuntime().exec(new String[]{"su","-c","reboot bootloader"});
Sent from my HTC One using xda premium
Not permitted
Not permitted!
Click to expand...
Click to collapse
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
pinpong said:
Runtime.getRuntime().exec(new String[]{"su","-c","reboot bootloader"});
Sent from my HTC One using xda premium
Click to expand...
Click to collapse
He wants to use RootTools.
For that use:
Code:
Command command = new Command(0, "reboot bootloader") {
[user=439709]@override[/user]
public void output(int id, String line) {
//TODO: Do something with the output here
}
};
RootTools.getShell(true).add(command).waitForFinish();
EDIT: @override: The first letter needs to be upper case (Doesn't let me as it thinks of it as a mentioning function.)
---------- Post added at 10:02 PM ---------- Previous post was at 10:00 PM ----------
rampo said:
Click to expand...
Click to collapse
First you need to be a root user.
Type "su" as the first command.
If you use RootTools you can execute commands as root without having to type "su" the way I gave you above.
Second adb commands must be launched from your computer.
nikwen said:
He wants to use RootTools.
For that use:
Code:
Command command = new Command(0, "reboot bootloader") {
[user=439709]@override[/user]
public void output(int id, String line) {
//TODO: Do something with the output here
}
};
RootTools.getShell(true).add(command).waitForFinish();
Click to expand...
Click to collapse
Same logcat : "Not permited!"
First you need to be a root user.
Click to expand...
Click to collapse
Don't warry man
Type "su" as the first command.
Click to expand...
Click to collapse
Already done.
If you use RootTools you can execute commands as root without having to type "su" the way I gave you above.
Click to expand...
Click to collapse
Ok, thanks.
Second adb commands must be launched from your computer.
Click to expand...
Click to collapse
Yes, i know, but i've tried
pinpong said:
Runtime.getRuntime().exec(new String[]{"su","-c","reboot bootloader"});
Sent from my HTC One using xda premium
Click to expand...
Click to collapse
Same "Not permited!"
rampo said:
Same logcat : "Not permited!"
Don't warry man
Already done.
Click to expand...
Click to collapse
In the screenshot you did not.
Ok, thanks.
Yes, i know, but i've tried
Click to expand...
Click to collapse
If I type "su", then press enter and then "reboot recovery", everything works fine.
Look at this for rebooting into bootloader: http://www.androidcentral.com/android-201-10-basic-terminal-commands-you-should-know
EDIT: You can see whether you typed "su" by looking at the last character of one line. If it is a # instead of a $, you typed "su".
EDIT: You can see whether you typed "su" by looking at the last character of one line. If it is a # instead of a $, you typed "su".
Click to expand...
Click to collapse
There is a "#"
If I type "su", then press enter and then "reboot recovery", everything works fine.
Click to expand...
Click to collapse
.
Not for me, i said this in my second post
HTC One, stock rooted ROM
rampo said:
There is a "#"
.
Not for me, i said this in my second post
HTC One, stock rooted ROM
Click to expand...
Click to collapse
Then it is because of the implementation of the reboot command of your ROM. On my Samsung Galaxy Ace with AOKP it is working fine.
Maybe you could compile your own busybox and include the reboot command. Then copy it into the data directory of your app and run the commands by typing their full path like this:
Code:
/data/data/com.example.app/files/busybox reboot recovery
Maybe you could compile your own busybox and include the reboot command. Then copy it into the data directory of your app and run the commands by typing their full path like this:
Click to expand...
Click to collapse
Mmm, if i add this myself-compiled busybox version to my app, this will work with all devices?
Because as you can read in the first post, the command should be written in the app I'm working on.
Or there is a universal another command or another way to do it?
rampo said:
Mmm, if i add this myself-compiled busybox version to my app, this will work with all devices?
Because as you can read in the first post, the command should be written in the app I'm working on.
Or there is a universal another command or another way to do it?
Click to expand...
Click to collapse
I really thought that you could use the "reboot recovery" on every device.
However, this is one of the best examples for different implementations on different devices. For example on some phones the "ls" command is linked to "ls -l" (Source). That is why busybox is always the better way. It is working the same on all devices and ROMs.
You need to compile it yourself because the one which is installed by this app does not include the reboot command.
I think that there is no other way as the "universal" did not work on your device. Maybe anotherone has a better idea, but I do not think so.
I really thought that you could use the "reboot recovery" on every device.
However, this is one of the best examples for different implementations on different devices. For example on some phones the "ls" command is linked to "ls -l" (Source). That is why busybox is always the better way. It is working the same on all devices and ROMs.
Click to expand...
Click to collapse
Thanks man, I've tried
Code:
su
reboot recovery
and
Code:
su
reboot bootloader
in my Galaxy Nexus, it works.
I will develop and test the app on Nexus, pure Google software
You could grab the reboot binary (I attached mine from the nexus 7), put it in /system/xbin then do this in terminal :
Code:
su
chmod 775 /system/xbin/reboot
After which, reboot/reboot-recovery/reboot-bootloader should work.
If this works, you can include the binary in the assets folder of your app, then copy it to /system/xbin and chmod it in your code.
Code:
File sdCard = Environment.getExternalStorageDirectory().toString();
File update = new File( sdCard + "reboot");
if (update.exists()) {
System.out.println("reboot binary already exists on sdcard");
} else {
try {
update.createNewFile();
System.out.println("File created successfully");
InputStream is = getAssets().open("reboot");
FileOutputStream fos = new FileOutputStream(update);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("reboot binary successfully moved to sdcard");
// Close the streams
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("busybox mount -o remount,rw /system"+"\n");
os.writeBytes("cp -f " + sdCard + "/reboot /system/xbin/reboot" + "\n");
os.writeBytes("chmod 775 /system/xbin/reboot" + "\n");
os.writeBytes("rm -f " + sdCard + "/reboot " + "\n");
os.writeBytes("busybox mount -o remount,ro /system"+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
private void rebootToRecovery() {
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("reboot recovery"+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
}
Damn, we really need syntax highlighting and indentation in the code forum tags...
Androguide.fr said:
You could grab the reboot binary (I attached mine from the nexus 7), put it in /system/xbin then do this in terminal :
Code:
su
chmod 775 /system/xbin/reboot
After which, reboot/reboot-recovery/reboot-bootloader should work.
If this works, you can include the binary in the assets folder of your app, then copy it to /system/xbin and chmod it in your code.
Code:
File sdCard = Environment.getExternalStorageDirectory();
sdCard.toString();
File update = new File( sdCard + "reboot");
if (update.exists()) {
System.out.println("reboot binary already exists on sdcard");
} else {
try {
update.createNewFile();
System.out.println("File created successfully");
InputStream is = getAssets().open("xbin");
FileOutputStream fos = new FileOutputStream(update);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("reboot binary successfully moved to sdcard");
// Close the streams
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("busybox mount -o remount,rw /system"+"\n");
os.writeBytes("cp -f " + sdCard + "/reboot /system/xbin/reboot" + "\n");
os.writeBytes("chmod 775 /system/xbin/reboot");
os.writeBytes("busybox mount -o remount,ro /system"+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
private void rebootToRecovery() {
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("reboot recovery"+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
}
Damn, we really need syntax highlighting and indentation in the code forum tags...
Click to expand...
Click to collapse
Genial
And there is a command for SafeMode reboot?
rampo said:
Genial
And there is a command for SafeMode reboot?
Click to expand...
Click to collapse
What do you mean by SafeMode ?
Androguide.fr said:
You could grab the reboot binary (I attached mine from the nexus 7), put it in /system/xbin then do this in terminal :
Code:
su
chmod 775 /system/xbin/reboot
After which, reboot/reboot-recovery/reboot-bootloader should work.
If this works, you can include the binary in the assets folder of your app, then copy it to /system/xbin and chmod it in your code.
Code:
File sdCard = Environment.getExternalStorageDirectory();
sdCard.toString();
File update = new File( sdCard + "reboot");
if (update.exists()) {
System.out.println("reboot binary already exists on sdcard");
} else {
try {
update.createNewFile();
System.out.println("File created successfully");
InputStream is = getAssets().open("xbin");
FileOutputStream fos = new FileOutputStream(update);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("reboot binary successfully moved to sdcard");
// Close the streams
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("busybox mount -o remount,rw /system"+"\n");
os.writeBytes("cp -f " + sdCard + "/reboot /system/xbin/reboot" + "\n");
os.writeBytes("chmod 775 /system/xbin/reboot" + "\n");
os.writeBytes("rm -f " + sdCard + "/reboot " + "\n");
os.writeBytes("busybox mount -o remount,ro /system"+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
private void rebootToRecovery() {
Process process = null;
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
os.writeBytes("reboot recovery"+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
}
Damn, we really need syntax highlighting and indentation in the code forum tags...
Click to expand...
Click to collapse
Really great idea. :good: Respect!
Just one thing I would do another way: I would copy the reboot binary to the data folder of the app so that the system is not modyfied.
If the default reboot binary has some additional functions which the system needs, there could go something wrong.
rampo said:
Click to expand...
Click to collapse
Simply type
Code:
su
reboot
PhAkEer said:
Simply type
Code:
su
reboot
Click to expand...
Click to collapse
nikwen said:
Really great idea. :good: Respect!
Just one thing I would do another way: I would copy the reboot binary to the data folder of the app so that the system is not modyfied.
If the default reboot binary has some additional functions which the system needs, there could go something wrong.
Click to expand...
Click to collapse
Yeah you're definitely right, would be better practice :good:
PhAkEer said:
Simply type
Code:
su
reboot
Click to expand...
Click to collapse
Dude, just read the thread...

Scripting Commands in Android App?

Hi All,
Things I am trying to do:
1. Connect to remote system
2. Run perl command and get the output on screen.
Could you please guide me on the process where in I can connect to a remote windows/unix machine and run build commands.
Any examples?
Thanks for your help.
Thanks,
Kino
Hi!
I've made something similar - An App that runs some shell scripts on an Ubuntu server over SSH.
Here are some code snippets, hope this helps you
Code:
JSch jsch = new JSch();
Session session = jsch.getSession([I]'ssh-username'[/I],IP, [I]'ssh-port'[/I]);
session.setPassword(SW); // SW ... password ssh-user (for me it's the same pw, ssh-user = su)
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
Channel channel = session.openChannel("exec");
Code:
if(SU) // If Command should run as SU
{
sudoCommand = "sudo -S -p '' " + command;
}
else
{
sudoCommand = command;
}
((ChannelExec) channel).setCommand(sudoCommand);
((ChannelExec) channel).setPty(true);
channel.connect();
OutputStream out = channel.getOutputStream();
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
channel.setOutputStream(responseStream);
if(SU)
{
out.write((SW + "\n").getBytes());
out.flush();
}
Thread.sleep(1000);
out.write(("logout" + "\n").getBytes());
out.flush();
Thread.sleep(500);
out.write(("exit" + "\n").getBytes());
out.flush();
out.close();
session.disconnect();
channel.disconnect();
Thank you LinuxForce. Thats helpful. I am trying to work on this.
LinuxForce said:
Hi!
I've made something similar - An App that runs some shell scripts on an Ubuntu server over SSH.
Here are some code snippets, hope this helps you
Click to expand...
Click to collapse

[Q] External process with ProcessBuilder

I wrote a simple application that needs to run external programs as superuser. I run this piece of code in a periodic task: the code is contained in a Runnable that runs periodically by using a ScheduledThreadPoolExecutor. The code works well for a while, but after some times it stops to work randomly. To do that I used the following code. By debugging the application I found the problem is in the line Process process = builder.start();
I mean that when the problem happens the execution wait the return of this method forever.
Code:
//Code in run() Method of a class that implemnst Runnable:
ArrayList<String> results = new ArrayList<String>();
try {
ProcessBuilder builder = new ProcessBuilder("su");
builder.redirectErrorStream(true);
Process process = builder.start();
DataOutputStream os = new DataOutputStream(
process.getOutputStream());
BufferedReader bRes = new BufferedReader(new InputStreamReader(
process.getInputStream()));
os.writeBytes("busybox ifconfig | busybox grep 'eth'; echo done_done\n"); //<-- this is my command for now
os.flush();
String r;
long curr = System.currentTimeMillis();
while ((r = bRes.readLine()).equals("done_done") == false){
results.add(r);
}
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
//...
}
Any suggestions? I can't figure out what kind of problems produce this situation. I used this code in past without problems. Can the command line "busybox ifconfig | busybox grep 'eth'; echo done_done\n" not return and works forever? I don't think so..
Thank you, Nico

Categories

Resources