以 root 身份运行本机 android 可执行文件?

发布于 2024-12-09 16:15:13 字数 842 浏览 1 评论 0原文

有人知道如何做到这一点吗? 这是我的代码:

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();
}

是的,我知道我没有进行任何测试来查看用户是否确实接受了超级用户对话框,但这只是对我自己的测试。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-12-16 16:15:13

这不会是您的完整解决方案,但这是我不久前尝试过的一些代码。它可以运行任意命令(代码在这里使用ls)。正如您从屏幕截图中看到的,ls 确实弹出了超级用户对话框。

在此处输入图像描述

我最初想做的是使用 sh 打开 shell然后在该 shell 中运行命令,以便仅显示初始 sh 超级用户对话框。我见过一些应用程序似乎可以做到这一点,但我仍然不确定它是如何完成的。我怀疑这也是您正在寻找的,但至少使用此代码您可以运行命令。 :)

无论如何,这是代码。将其粘贴到 Android 应用程序框架中,它应该可以工作。

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());
}

祝你好运!

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.

enter image description here

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!

捂风挽笑 2024-12-16 16:15:13

刚刚测试过,它无法在 Nexus S、操作系统 2.3.6 上运行。

更新

既然你的手机已经root,su应该可以工作。您看到的问题是密码对话框不显示。这似乎适用于某些已 root 的手机,但可能是自定义固件的功能。

您可以做的是启动 su 进程,通过对话框询问用户密码,然后将其输出到 su 进程。

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

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

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);
玩心态 2024-12-16 16:15:13

我自己从未尝试过,但是如果您能够在没有 su 权限的应用程序中以 su 的方式执行操作,那么这没有多大意义。所以我想您需要做的第一件事就是为您的应用程序请求 su 权限:

// 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);

然后您可以:(

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

复制自 http://forum.xda-developers.com/showpost.php?p=2954887&postcount=7)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文