admin 管理员组

文章数量: 887021


2024年1月18日发(作者:python语言程序设计教程赵璐)

Android下执行脚本

/**

* Internal thread used to execute scripts (as root or not).

*/

private static final class ScriptRunner extends Thread {

private final File file;

private final String script;

private final StringBuilder res;

private final boolean asroot;

public int exitcode = -1;

private Process exec;

/**

* Creates a new script runner.

* @param file temporary script file

* @param script script to run

* @param res response output

* @param asroot if true, executes the script as root

*/

public ScriptRunner(File file, String script, StringBuilder res, boolean asroot) {

= file;

= script;

= res;

= asroot;

}

@Override

public void run() {

try {

NewFile();

final String abspath = olutePath();

// make sure we have execution permission on the script file

time().exec("chmod 777 "+abspath).waitFor();

// Write the script to be executed

final OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file));

("#!/system/bin/shn");

(script);

if (!th("n")) ("n");

("exitn");

();

();

if () {

// Create the "su" request to run the script

exec = time().exec("su -c "+abspath);

} else {

// Create the "sh" request to run the script

exec = time().exec("sh "+abspath);

}

InputStreamReader r = new InputStreamReader(utStream());

final char buf[] = new char[1024];

int read = 0;

// Consume the "stdout"

while ((read=(buf)) != -1) {

if (res != null) (buf, 0, read);

}

// Consume the "stderr"

r = new InputStreamReader(orStream());

read=0;

while ((read=(buf)) != -1) {

if (res != null) (buf, 0, read);

}

// get the process exit code

if (exec != null) de = r();

} catch (InterruptedException ex) {

if (res != null) ("nOperation timed-out");

} catch (Exception ex) {

if (res != null) ("n" + ex);

} finally {

destroy();

}

}

/**

* Destroy this script runner

*/

public synchronized void destroy() {

if (exec != null) y();

exec = null;

}

}

/**

* Runs a script, wither as root or as a regular user (multiple commands separated by

"n").

* @param ctx mandatory context

* @param script the script to be executed

* @param res the script output response (stdout + stderr)

* @param timeout timeout in milliseconds (-1 for none)

* @return the script exit code

*/

public static int runScript(Context ctx, String script, StringBuilder res, long timeout,

boolean asroot) {

final File file = new File(heDir(), SCRIPT_FILE);

final ScriptRunner runner = new ScriptRunner(file, script, res, asroot);

();

try {

if (timeout > 0) {

(timeout);

} else {

();

}

if (e()) {

// Timed-out

upt();

(150);

y();

(50);

}

} catch (InterruptedException ex) {}

return de;

}

/**

* Runs a script as root (multiple commands separated by "n").

* @param ctx mandatory context

* @param script the script to be executed

* @param res the script output response (stdout + stderr)

* @param timeout timeout in milliseconds (-1 for none)

* @return the script exit code

*/

public static int runScriptAsRoot(Context ctx, String script, StringBuilder res, long timeout)

{

return runScript(ctx, script, res, timeout, true);

}

private static boolean hasroot = false;

/**

* Check if we have root access

* @param ctx mandatory context

* @return boolean true if we have root

*/

public static boolean hasRootAccess(Context ctx) {

if (hasroot) return true;

final StringBuilder res = new StringBuilder();

try {

// Run an empty script just to check root access

if (runScriptAsRoot(ctx, "exit 0", res) == 0) {

hasroot = true;

return true;

}

} catch (Exception e) {

}

return false;

}


本文标签: 语言 程序设计 脚本 执行 作者