android 运行shell 脚本文件或shell命令

2024-04-08 1462阅读

android 运行shell 脚本文件或shell命令

android 运行shell 脚本文件或shell命令
(图片来源网络,侵删)

一.运行shell脚本文件

1.test.sh文件内容

#!/bin/bash

echo "I am a script"

ps

2.将shell文件拷贝到Android设备目录

3.执行脚本文件 Runtime.getRuntime().exec("sh /sdcard/lilei/test.sh");

注:应用需要有存储访问权限,如果shell文件中有文件访问请用绝对路径,否则访问不到

二.运行shell命令

调用Runtime.getRuntime().exec(String cmdarray[]) ,这里传递命令数组或者Runtime.getRuntime().exec(String command)直接执行命令。

注:有些shell命令可以在 adb shell 中运行,但是通过应用Runtime执行命令会失败(即使当前已经是uid system权限)当前测试系统版本Android11.

例如 adb shell中可以执行执命令:pm install -r -t /data/local/tmp/test-debug.apk 安装应用

但是通过代码运行无法安装:Process p = Runtime.getRuntime().exec("pm install -r -t /data/local/tmp/test-debug.apk");

系统日志会报错01-09 15:35:45.548 18925 18925 W cmd : type=1400 audit(0.0:580): avc: denied { read } for name="test-debug.apk" dev="vdb" ino=1130534 scontext=u:r:system_app:s0 tcontext=u:object_r:shell_data_file:s0 tclass=file permissive=0

缺少什么权限:     { read }权限,

谁缺少权限:        scontext=u:r:system_app:s0 

对哪个文件缺少权限: tcontext=u:object_r:shell_data_file:s0

什么类型的文件:  tclass=file 

完整的意思: scontext=u:r:system_app:s0进程对shell_data_file:s0类型的file缺少read 权限。

可执行adb root后再执行:

setenforce 0 (临时禁用掉SELinux)

getenforce (得到结果为Permissive)

上述命令执行完之后,代码就可以执行安装命令了,基本可以确认是SELinux造成的权限问题,需要通过正规的方式来解决权限问题。

-----------------具体实现如下--------------------------------

/**

* date: 04/23/2020.

* author: lilei.

*/

public class RuntimeUtil {

private static final String TAG = AppConstants.APP_TAG + "RuntimeUtil ";

private static final String SAVE_LOG_DIR_PATH = AppConstants.TSP_DIR + "/log/";

/**

* 测试运行shell 脚本文件

* @return

*/

public static String testRunShell() {

Log.d(TAG, "testRunShell 11");

String cmd = "ps";

try {

java.lang.Process p = Runtime.getRuntime().exec("sh /sdcard/lilei/test.sh");

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = null;

int index = 0;

while ((line = in.readLine()) != null) {

Log.d(TAG, "testRunShell 22 index:"+index +" line:"+line);

index++;

}

} catch (IOException e) {

Log.d(TAG, "testRunShell err=" + e.toString());

}

Log.d(TAG, "testRunShell 33");

return null;

}

/**

* getRetrieveLog from cmd adb logcat.

* for log test.

*

* @param timeMillis timeMillis.

* @param line if line is 0,set recent 5 second time.

*/

public static void getRetrieveLog(long timeMillis, String line) {

String retrieveTime = DateTimeUtil.getBeforeMillisDateTime(timeMillis, 5000);

Log.d(TAG, "getRetrieveLog() begin retrieveTime:" + retrieveTime

+ " line:" + line);

try {

ArrayList cmdLine = new ArrayList();

cmdLine.add("logcat");

cmdLine.add("-t");

if ("0".equals(line)) {

cmdLine.add(retrieveTime);

} else {

cmdLine.add(line);

}

Log.d(TAG, "getRetrieveLog() cmdLine:" + cmdLine.toString());

Process process = Runtime.getRuntime().exec(

cmdLine.toArray(new String[cmdLine.size()]));

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(process.getInputStream()));

String str = null;

while ((str = bufferedReader.readLine()) != null) {

Log.d(TAG, "getRetrieveLog() str:" + str);

}

if (str == null) {

Log.d(TAG, "getRetrieveLog() end!!-- is null --");

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* getRetrieveLogToFile from cmd adb logcat.

*

* @param timeMillis current time.

* @param recentSeconds recent seconds.

* @param fileName file name.

* @return Full log path.

*/

public static String getRetrieveLogToFile(long timeMillis, int recentSeconds, String fileName) {

String retrieveTime = DateTimeUtil.getBeforeMillisDateTime(

timeMillis, recentSeconds * 1000);

Log.d(TAG, "getRetrieveLogToFile() begin UTF-8 timeMillis:" + timeMillis

+ " recentSeconds:" + recentSeconds + " begin retrieveTime:" + retrieveTime);

BufferedWriter bufferedWriter = null;

ArrayList cmdLine = new ArrayList();

cmdLine.add("logcat");

cmdLine.add("-t");

cmdLine.add(retrieveTime);

Log.d(TAG, "getRetrieveLog() cmdLine:" + cmdLine.toString());

Process process = null; //capture log.

String fullLogPath = null;

try {

//create a new path.

File dirFile = new File(SAVE_LOG_DIR_PATH);

if (!dirFile.exists()) {

dirFile.mkdirs();

}

process = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(

process.getInputStream()));

fullLogPath = SAVE_LOG_DIR_PATH + fileName;

bufferedWriter = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(fullLogPath), StandardCharsets.UTF_8));

int len = 0;

byte[] buf = new byte[1024];

String str = null;

while ((str = bufferedReader.readLine()) != null) {

//Start reading the log, one line at a time.

//Log.d(TAG, "getRetrieveLogToFile() str:" + str);

bufferedWriter.write(str + "\r\n");

}

} catch (IOException e1) {

Log.d(TAG, "getRetrieveLogToFile() error:" + e1);

e1.printStackTrace();

} catch (Exception e) {

Log.d(TAG, "getRetrieveLogToFile() error:" + e);

e.printStackTrace();

} finally {

if (null != bufferedWriter) {

try {

bufferedWriter.close();

bufferedWriter = null;

} catch (IOException e) {

Log.e(TAG, "getRetrieveLogToFile() error:" + e);

}

}

}

Log.d(TAG, "getRetrieveLogToFile() end!!");

return fullLogPath;

}

}

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]