开发者

Running native android executable as root?

开发者 https://www.devze.com 2023-04-12 12:10 出处:网络
Anybody knows how to to this? This is my code: try { Process p = Runtime.getRuntime().exec(\"su\"); 开发者_开发知识库p.waitFor();

Anybody knows how to to this? This is my code:

try {
            Process p = Runtime.getRuntime().exec("su");
 开发者_开发知识库           p.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            Process p = Runtime.getRuntime().exec(
                    "/system/bin/netcfg > /sdcard/netcfg.txt");
            p.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
}

Yes, I know I haven't got any tests to see if user actually accepted the superuser dialog, but this is just a test for myself.


This won't be your complete solution, but this is some code I was experimenting with a while ago. It CAN run an arbitrary command (the code uses ls here). As you can see from the screenshot, the superuser dialog does pop up for ls.

Running native android executable as root?

What I had originally been trying to do was to open a shell with sh and then run commands within that shell so that only the initial sh superuser dialog would every be shown. I've seen apps that seem to do this, but I'm still unsure how it is done. I suspect that is what you're looking for as well, but at least with this code you can run commands. :)

Anyway, here's the code. Stick it into a skeleton Android app and it should work.

public class ShellCommandTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final StringBuilder log = new StringBuilder();
    try{
        ArrayList<String> commandLine = new ArrayList<String>();
        commandLine.add("su");
        commandLine.add("-c");
        commandLine.add("ls");

        Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        for (String command : commandLine){
            Log.i("SU_Test", command);
        }

        String line;
        while ((line = bufferedReader.readLine()) != null){ 
            log.append(line);
            log.append("\n"); 
        }
    } 
    catch (IOException e){
        Log.e("SU_Test", "Fail: " + e.getMessage());
    } 
    Log.i("SU_Test", log.toString());
}

Good luck!


Just tested it and it does NOT work on Nexus S, OS 2.3.6.

Update

SInce your phone is rooted su should work. The problem that you are seeing is that password dialog does not show. This seems to work on some rooted phones, but is probably a feature of custom firmware.

What you can do is start the su process, ask user for password via a Dialog and then output it to su process.

 Process process = new ProcessBuilder()
   .command("su", "android.com")
   .redirectErrorStream(true)
   .start();

// Dialog for asking pass here
OutputStream out = process.getOutputStream();
out.write(password);


I've never tried myself, but it wouldnt make a lot of sense for you to be able to do stuff as su from within an app without su rights. So I imagine the first thing you need to do is request su rights for your app:

// arbitrary number of your choosing
final int SUPERUSER_REQUEST = 2323; 

// superuser request
Intent intent = new Intent("android.intent.action.superuser"); 

// tell Superuser the name of the requesting app
intent.putExtra("name", "Shell"); 

// tel Superuser the name of the requesting package
intent.putExtra("packagename", "koushikdutta.shell"); 

// make the request!
startActivityForResult(intent, SUPERUSER_REQUEST);

Then you can:

Runtime.getRuntime().exec("su -c <your privileged command here>");

(copied from http://forum.xda-developers.com/showpost.php?p=2954887&postcount=7)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号