iis服务器助手广告
返回顶部
首页 > 资讯 > 移动开发 >Android 代码执行shell指令
  • 925
分享到

Android 代码执行shell指令

android服务器java 2023-09-01 08:09:46 925人浏览 泡泡鱼
摘要

Android 执行shell指令 方式一:此方式如果执行的代码带echo开头,会没有作用。可能是echo这个指令会被优先执行。     public static void executeCommand(String command)

Android 执行shell指令

方式一:此方式如果执行的代码带echo开头,会没有作用。可能是echo这个指令会被优先执行。

    public static void executeCommand(String command) {

        Runtime mRuntime = Runtime.getRuntime();

        try {

            //Process中封装了返回的结果和执行错误的结果

            Process mProcess = mRuntime.exec(command);

            BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));

            StringBuffer mRespBuff = new StringBuffer();

            char[] buff = new char[1024];

            int ch = 0;

            while ((ch = mReader.read(buff)) != -1) {

                mRespBuff.append(buff, 0, ch);

            }

            mReader.close();

            System.out.print(mRespBuff.toString());

        } catch (ioException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

方式二:可以执行echo开头或者ping 开头的shell指令代码

Runtime mRuntime = Runtime.getRuntime();

mRuntime.exec(new String[] {"/bin/sh", "-c", "echo 1 > /sys/class/sensor_class/accel_calibration"});

 

一个工具类:

public class ShellUtils {
    private static final String TAG = "ShellUtils";

    public static final String COMMAND_SU = "su";
    public static final String COMMAND_SH = "sh";
    public static final String COMMAND_EXIT = "exit\n";
    public static final String COMMAND_LINE_END = "\n";

    private ShellUtils() {
        throw new AssertionError();
    }

   
    public static boolean checkRootPermission() {
        return execCommand("echo root", true, false).result == 0;
    }

   
    public static CommandResult execCommand(String command, boolean isRoot) {
        return execCommand(new String[] { command }, isRoot, true);
    }

   
    public static CommandResult execCommand(List commands,
                                            boolean isRoot) {
        return execCommand(
                commands == null ? null : commands.toArray(new String[] {}),
                isRoot, true);
    }

   
    public static CommandResult execCommand(String[] commands, boolean isRoot) {
        return execCommand(commands, isRoot, true);
    }

   
    public static CommandResult execCommand(String command, boolean isRoot,
                                            boolean isNeedResultMsg) {
        return execCommand(new String[] { command }, isRoot, isNeedResultMsg);
    }

   
    public static CommandResult execCommand(List commands,
                                            boolean isRoot, boolean isNeedResultMsg) {
        return execCommand(
                commands == null ? null : commands.toArray(new String[] {}),
                isRoot, isNeedResultMsg);
    }

   
    public static CommandResult execCommand(String[] commands, boolean isRoot,
                                            boolean isNeedResultMsg) {
        int result = -1;
        if (commands == null || commands.length == 0) {
            return new CommandResult(result, null, null);
        }

        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder succeSSMsg = null;
        StringBuilder errORMsg = null;

        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command == null) {
                    continue;
                }
                // donnot use os.writeBytes(commmand), avoid chinese charset
                // error
                os.write(command.getBytes());
                os.writeBytes(COMMAND_LINE_END);
                os.flush();
            }
            os.writeBytes(COMMAND_EXIT);
            os.flush();
            result = process.waitFor();
            // get command result
            if (isNeedResultMsg) {
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = new BufferedReader(new InputStreamReader(
                        process.getInputStream()));
                errorResult = new BufferedReader(new InputStreamReader(
                        process.getErrorStream()));
                String s;
                while ((s = successResult.readLine()) != null) {
                    successMsg.append(s);
                }
                while ((s = errorResult.readLine()) != null) {
                    errorMsg.append(s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return new CommandResult(result, successMsg == null ? null
                : successMsg.toString(), errorMsg == null ? null
                : errorMsg.toString());
    }

   
    public static class CommandResult {
       
        public int result;
       
        public String successMsg;
       
        public String errorMsg;

        public CommandResult(int result) {
            this.result = result;
        }

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }
    }
}
 

来源地址:https://blog.csdn.net/MilesMatheson/article/details/130759655

--结束END--

本文标题: Android 代码执行shell指令

本文链接: https://www.lsjlt.com/news/387925.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android 代码执行shell指令
    Android 执行shell指令 方式一:此方式如果执行的代码带echo开头,会没有作用。可能是echo这个指令会被优先执行。     public static void executeCommand(String command)...
    99+
    2023-09-01
    android 服务器 java
  • Android代码执行ADB指令
    要在Android代码中执行ADB指令,你可以使用Java的ProcessBuilder类来创建一个子进程来执行命令。以下是一个示例代码: import java.io.BufferedReader;import java.io.IOExc...
    99+
    2023-09-05
    android adb
  • android app代码中执行adb指令
    方案1:使用Runtime类 public static String execRootCmd(String cmd) { String content = ""; try { ...
    99+
    2023-09-06
    android adb java
  • python os.system执行cmd指令代码详解
    1、执行cmd指令,在cmd输出的内容会直接在控制台输出,返回结果为0表示执行成功。 2、在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用脚本的信号号码,高位为脚...
    99+
    2024-04-02
  • Java执行shell命令
    Java执行shell命令 前言一、案例场景原因解决方案 二、拓展创建临时脚本,执行shell命令 三、总结 前言 java执行shell命令的方式有很多种,但是在应...
    99+
    2023-09-05
    java 开发语言 linux
  • linux中shell如何执行用户的指令
    本篇内容介绍了“linux中shell如何执行用户的指令”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!linux中shell是“壳”的意思,...
    99+
    2023-06-22
  • Python:执行命令行指令
    文章目录 简介os.systemos.popensubprocess.Popen()参考文献 简介 在python中,调用外部命令行(linux中的shell、或者windows中的cmd...
    99+
    2023-09-30
    python
  • PHP-代码执行函数-命令执行函数
    目录 代码执行函数: 1.eval()函数 2.assert()  函数 3.call_user_func()函数  4- create_function()函数  5- array_map()函数 6- call_user_func_ar...
    99+
    2023-09-09
    php 开发语言 网络安全
  • python 之 shell命令执行
    python中有几种常用的执行shell命令的模块1,os.system()2, os.popen()3,pexpect.run()下面介绍3个模块的差别1,os.system() 直接执行>>> os.system('l...
    99+
    2023-01-31
    命令 python shell
  • python之执行shell命令
    [root@s141 ~]# python Python 2.7.5 (default, Sep 15 2016, 22:37:39)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Ty...
    99+
    2023-01-31
    命令 python shell
  • Web漏洞-命令执行和代码执行漏洞
    命令执行原理 就是指用户通过浏览器或其他辅助程序提交执行命令,由于服务器端没有针对执行函数做过滤,导致在没有指定绝对路径的情况下就执行命令。 漏洞成因 它所执行的命令会继承WebServer的权限,也就是说可以任意读取、修改、执行We...
    99+
    2023-08-31
    网络安全 web安全 Powered by 金山文档
  • Linux命令行循环执行shell命令
    目录linux命令行,循环执行shell命令死循环命令格式效果普通计数循环循环10次效果LLinux命令行,循环执行shell命令 死循环 命令格式 while true ;do <command>; don...
    99+
    2023-01-03
    linuxshell循环执行命令 循环执行shell命令 linux命令行 Linuxshell循环命令 while死循环
  • RCE代码及命令执行(详解)
    RCE代码及命令执行 1.RCE漏洞1.1.漏洞原理1.2.漏洞产生条件1.3.漏洞挖掘1.4.漏洞分类1.4.1.命令执行1.4.1.1.漏洞原理1.4.1.2.命令执行危险函数1.4.1....
    99+
    2023-09-11
    php web安全 安全 网络安全
  • iOS快捷指令:执行Python脚本(利用iSH Shell)
    文章目录 前言核心逻辑配置iSH安装Python创建Python脚本配置启动文件测试效果 快捷指令 前言 iOS快捷指令所能做的操作极为有限。假如快捷指令能运行Python程序,那么可操作空间就瞬间变大了。iSH是一款...
    99+
    2023-08-30
    python ios 开发语言
  • python怎么执行shell命令
    在Python中可以使用`os`模块中的`system`函数来执行Shell命令。以下是一个示例:```pythonimport o...
    99+
    2023-09-27
    python shell
  • Python 执行Shell 外部命令
    1、os.system()此方法执行的外部程序,会将结果直接输出到标准输出。os.system的返回结果为执行shell 的 $ 值。因此请执行没有输出结果的程序时适合使用此方法。如touch 、rm 一个文件等。In [1]: impor...
    99+
    2023-01-31
    命令 Python Shell
  • python 执行 shell命令 的几
        os.system 最近有个需求就是页面上执行shell命令,第一想到的就是os.system os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0 或者 1,当然不满足需求了。 ...
    99+
    2023-01-31
    命令 python shell
  • 使用命令行怎么执行PHP代码
    这篇文章将为大家详细讲解有关使用命令行怎么执行PHP代码,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Windows 用户的 PHP 安装:按照步骤在 Windows 操作系统上安装 PHP...
    99+
    2023-06-15
  • linux下如何执行shell命令
    这篇文章将为大家详细讲解有关linux下如何执行shell命令,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。linux下执行shell命令有两种方法 在当前shell中执行shell命令在当前s...
    99+
    2023-06-09
  • nodejs脚本中执行shell命令
    nodejs脚本中执行shell命令 官方文档一:exec 方法执行shell命令1. _注意:2. _优点特性:3. _语法格式:4. _option对象属性:5. _示例: 二:spa...
    99+
    2023-08-31
    linux unix 服务器
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作