`
fonter
  • 浏览: 857979 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ShellCommand.java

阅读更多

/**
 * ShellCommand.java runs commands as if in a native shell instance, and can return stdio.
 *
 * Code by Kevin(at)TeslaCoil
 * Adapted by LouZiffer(at)SDX
 *
 * Example usage (use cmd.su.runWaitFor instead of cmd.sh.runWaitFor to run as su):
 *
 * ShellCommand cmd = new ShellCommand();
 * CommandResult r = cmd.sh.runWaitFor("/system/bin/getprop wifi.interface");
 *
 * if (!r.success()) {
 *     Log.d(MSG_TAG, "Error " + r.stderr);
 * } else {
 *     Log.d(MSG_TAG, "Successfully executed getprop wifi.interface. Result: " + r.stdout);
 *     this.tetherNetworkDevice = (r.stdout);
 * }
 */

package m900.tether.system;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;

import android.util.Log;

public class ShellCommand {
    private static final String TAG = "ShellCommand.java";
    private Boolean can_su;   

    public SH sh;
    public SH su;
   
    public ShellCommand() {
        sh = new SH("sh");
        su = new SH("su");
    }
   
    public boolean canSU() {
        return canSU(false);
    }
   
    public boolean canSU(boolean force_check) {
        if (can_su == null || force_check) {
            CommandResult r = su.runWaitFor("id");
            StringBuilder out = new StringBuilder();
           
            if (r.stdout != null)
                out.append(r.stdout).append(" ; ");
            if (r.stderr != null)
                out.append(r.stderr);
           
            Log.v(TAG, "canSU() su[" + r.exit_value + "]: " + out);
            can_su = r.success();
        }
        return can_su;
    }

    public SH suOrSH() {
        return canSU() ? su : sh;
    }
   
    public class CommandResult {
        public final String stdout;
        public final String stderr;
        public final Integer exit_value;
       
        CommandResult(Integer exit_value_in, String stdout_in, String stderr_in)
        {
            exit_value = exit_value_in;
            stdout = stdout_in;
            stderr = stderr_in;
        }
       
        CommandResult(Integer exit_value_in) {
            this(exit_value_in, null, null);
        }
       
        public boolean success() {
            return exit_value != null && exit_value == 0;
        }
    }

    public class SH {
        private String SHELL = "sh";

        public SH(String SHELL_in) {
            SHELL = SHELL_in;
        }

        public Process run(String s) {
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(SHELL);
                DataOutputStream toProcess = new DataOutputStream(process.getOutputStream());
                toProcess.writeBytes("exec " + s + "\n");
                toProcess.flush();
            } catch(Exception e) {
                Log.e(TAG, "Exception while trying to run: '" + s + "' " + e.getMessage());
                process = null;
            }
            return process;
        }
       
        private String getStreamLines(InputStream is) {
            String out = null;
            StringBuffer buffer = null;
            DataInputStream dis = new DataInputStream(is);

            try {
                if (dis.available() > 0) {
                    buffer = new StringBuffer(dis.readLine());
                    while(dis.available() > 0)
                        buffer.append("\n").append(dis.readLine());
                }
                dis.close();
            } catch (Exception ex) {
                Log.e(TAG, ex.getMessage());
            }
            if (buffer != null)
                out = buffer.toString();
            return out;
        }

        public CommandResult runWaitFor(String s) {
            Process process = run(s);
            Integer exit_value = null;
            String stdout = null;
            String stderr = null;
            if (process != null) {
                try {
                    exit_value = process.waitFor();
                   
                    stdout = getStreamLines(process.getInputStream());
                    stderr = getStreamLines(process.getErrorStream());
                   
                } catch(InterruptedException e) {
                    Log.e(TAG, "runWaitFor " + e.toString());
                } catch(NullPointerException e) {
                    Log.e(TAG, "runWaitFor " + e.toString());
                }
            }
            return new CommandResult(exit_value, stdout, stderr);
        }
    }
}

分享到:
评论

相关推荐

    NativeIO和YARNRunner修改后的源码

    at org.apache.hadoop.util.Shell.runCommand(Shell.java:538) at org.apache.hadoop.util.Shell.run(Shell.java:455).... 这是hadoop本身的一个bug,可以通过修改NativeIO和YARNRunner的源码并替换解决。这是这两个...

    hadoop-2.6.0-hadoop.dll-winutils.exe

     at org.apache.hadoop.util.Shell.runCommand(Shell.java:482)  at org.apache.hadoop.util.Shell.run(Shell.java:455)  at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:715)  at ...

    Shell run Java

    在Unix Shell脚本里运行Java application

    'FrontEnd Plus' The GUI for the fast JAva Decompiler.

    It is handled by Jad rather than the command shell, so on UNIX the last argument should be single-quoted: jad -o -r -sjava -dsrc 'tree/**/*.class' In a case you want to check the accuracy of the ...

    AdbShellCommand:在Android项目中执行adb Shell命令,通过java代码调用执行,采用MVP架构演示样例

    ##AdbShellCommand在Android项目中执行adb Shell命令,通过java代码调用执行,本程序采用MVP架构。###Java中执行adb shell命令本项目中,adb shell命令执行的实现方法主要参考网文()。文章中,把执行代码集成在...

    hadoop.dll

    at org.apache.hadoop.util.Shell.runCommand(Shell.java:482) 2:window10操作系统下面。hadoop-2.6.4版本,解决上面这个问题的必须两个文件。详细见博客:http://www.cnblogs.com/biehongli/p/7895857.html 3:...

    java调用shell脚本执行sqlldr与存储过程

    在java代码中调用执行shell脚本,sqlldr导数与使用sqlplus在shell调用执行存储过程。 linux环境中有2个dba的用户:oracle、erm 还有1个web用户:erm 在linux环境中,这三个用户都可以直接在任意目录下执行该shell...

    Java.to.Python 高清完整epub版

    But Python is just as powerful as Java. If Java is the heavy metal of computer programming, then Python is the jazz that opens doors of freedom in software development. Both Java and Python are ...

    Perl Debugged.pdf

    Table of Content Table of Content.........................................................................................................................i Copyright....................................

    Debugging with GDB --2007年

    2.3 Shell commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 5 6 6 6 6 6 7 7 7 8 11 11 13 15 15 16 GDB Commands . . . . . . . . . . . . . . . . . . . . . . . . ...

    Java反编译软件JAD1

    It is handled by Jad rather then command shell, so on UNIX the last argument should be single-quoted: jad -o -r -sjava -dsrc 'tree/**/*.class' In case you want to check the accuracy of the ...

    android-a programmer's guide

    6 Using the Command-Line Tools and the Android Emulator . . . . . . . . . . . . 83 Creating a Shell Activity Using the Windows CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Running ...

    测试平台模块phoenix-telnetclient.zip

    shell - start shell command ex:!shell sh test.sh 1116.!export - export a file ,if filepath have block,use '^' instead,ex:!export e:\test\test.txt isappend(true/false) datas7.!file - return a file ...

    shake:clojure库震撼了您的外壳

    any shell command ... ( sh/uname -a) ; ;returns a # ; ; using clojure variables (vars, local bindings) in shake ( let [home " /home/sunng87 " ] ( sh/ls -l $home)) ; ; using clojure forms in shake ( ...

    金蝶BOSV6.1_业务组件API参考手册

    Packages ...com.kingdee.bos.framework.command ...com.kingdee.bos.util.backport.concurrent.helpers Auxiliary and helper classes for backport.util.concurrent, NOT present in java.util.concurrent. ...

    Android静默安装常用工具类

    static HttpResponse httpGet(java.lang.String httpUrl) static String httpGetString(String httpUrl) 包含以上三个方法,默认使用gzip压缩,使用bufferedReader提高读取速度。 HttpRequest中可以设置url、timeout...

    db2数据库入门教程(官方中文版)

    PART I – 概览.........................................................................................................................11 第 1章 – DB2 Express-C是什么?..................................

    java 修改默认浏览器

    1.找到HKEY_CLASSES_ROOT\http\shell\open\command,双击"默认",将要用浏览器的可执行文件的完全路径输入到这里,例如:输入“C:\Program Files\Internet Explorer\iexplore.exe”; 2.然后找到HKEY_CLASSES_ROOT\...

    java反编译工具jad 1.5.8g(可以反编译jdk1.5,1.6)

    It is handled by Jad rather than the command shell, so on UNIX the last argument should be single-quoted: <br> jad -o -r -sjava -dsrc 'tree/**/*.class' <br> In a case you want to check ...

    java程序双击运行-双击jar文件运行程序.pdf

    5) 在注册表编辑器中,找到"HKEY_CLASSES_ROOT\Applications\javaw.exe\shell\open\command",在其中⽂件打开命令中加 ⼊参数"-jar"(⽆引号),修改后的数值类似:""C:\ProgramFiles\Java\jre7\bin\javaw.exe" -jar ...

Global site tag (gtag.js) - Google Analytics