android su执行命令

通过Runtime.getRuntime().exec("su")启动su进程,然后使用DataOutputStream向其写入要执行的命令。命令执行完成后需要调用process.waitFor()等待进程结束,并妥善关闭所有流资源

复制代码
import java.io.DataOutputStream;

public class SuCommandExecutor {
    private static final String TAG = "SuCommandExecutor";
    
    /**
     * 执行需要root权限的单个命令
     * @param command 要执行的命令
     * @return 命令执行结果
     */
    public static String execSuCmd(String command) {
        String output = "";
        Process process = null;
        DataOutputStream os = null;
        DataInputStream is = null;
        
        try {
            // 启动su进程获取root权限
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            
            // 写入要执行的命令
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            
            // 等待命令执行完成
            int exitCode = process.waitFor();
            
            // 读取命令输出
            is = new DataInputStream(process.getInputStream());
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            output = new String(buffer);
            
        } catch (Exception e) {
            e.printStackTrace();
            output = "Exception: " + e.getMessage();
        } finally {
            // 关闭所有流资源
            try {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return output;
    }
    
    /**
     * 静默执行root命令(不读取输出)
     * @param command 要执行的命令
     * @return 执行结果代码
     */
    public static int execRootCmdSilent(String command) {
        int result = -1;
        DataOutputStream dos = null;
        
        try {
            Process process = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes(command + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            process.waitFor();
            result = process.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    
    /**
     * 测试设备是否具有su执行权限
     * @return 测试结果
     */
    public static boolean testSuPermission() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes("id\n");
            dos.writeBytes("exit\n");
            dos.flush();
            int exitCode = process.waitFor();
            return exitCode == 0;
        } catch (Exception e) {
            return false;
        }
    }
    
    public static void main(String[] args) {
        // 测试su权限
        boolean hasPermission = testSuPermission();
        System.out.println("设备是否具有su权限: " + hasPermission);
        
        if (hasPermission) {
            // 执行示例命令
            String result = execSuCmd("ls -l /system");
            System.out.println("命令执行结果: " + result);
        }
    }
}

import java.io.DataOutputStream;

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream dos = new DataOutputStream(process.getOutputStream());
                    dos.writeBytes("echo 72 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo in > /sys/class/gpio/gpio72/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 78 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo in > /sys/class/gpio/gpio78/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 98 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo in > /sys/class/gpio/gpio98/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 97 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo out > /sys/class/gpio/gpio97/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 62 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo out > /sys/class/gpio/gpio62/direction"+"\n");
                    dos.flush();
        
                    dos.writeBytes("echo 63 > /sys/class/gpio/export"+"\n");
                    dos.flush();
                    dos.writeBytes("echo out > /sys/class/gpio/gpio63/direction"+"\n");
                    dos.flush();
        
                    dos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

关键注意事项

  • 设备要求‌:执行su命令前必须确认设备已root,且应用获得了su执行权限。
  • 权限申请‌:首次执行su命令时,系统会弹出授权对话框请求用户确认。
  • 异常处理‌:必须妥善处理IOException等异常情况,特别是在某些Android主板上可能出现执行异常4。
  • 资源管理‌:使用完毕后必须关闭DataOutputStream和Process等资源,避免内存泄漏。
  • 系统限制‌:普通应用无法直接访问系统自带的su程序,需要通过SuperSU等工具或系统root来获取权限。

该实现提供了完整的su命令执行功能,包括权限测试、命令执行和异常处理,能够满足Android应用中执行root权限命令的需求。

相关推荐
搜狐技术产品小编20231 分钟前
破局与重构:纯端侧 Android 自动化引擎的尝试与未来推演
android·运维·重构·自动化
bsauce8 分钟前
【kernel exploit】CVE-2026-23271 perf_event竞态UAF漏洞-ROP提权
linux·linux内核·内核漏洞·内核漏洞利用
深圳市晶科鑫实业有限公司12 分钟前
5G与AIoT时代:如何选择晶振常用频率?
服务器·单片机·物联网·5g·智能路由器·健康医疗·信息与通信
三十..14 分钟前
Ceph分布式存储核心技术精要与运维实践指南
运维·分布式·ceph
tianyuanwo25 分钟前
Jenkins × Gerrit 集成:自动触发构建的全流程解析
运维·servlet·jenkins
Hehuyi_In40 分钟前
从优雅到爆烈 —— Linux全力回收内存的一生
linux·内核·内存·memory·回收
顾默@1 小时前
双系统Ubuntu18.04升级22.04,安装docker进行openclaw安装
运维·docker·容器
杨充1 小时前
1.1 数据编码设计原理
linux·运维·网络·底层原理·数据编码
fan_music1 小时前
Linux I/O
linux
一只鹿鹿鹿1 小时前
信息化项目管理规范(参考Word文件)
java·大数据·运维·开发语言·数据库