繁体   English   中英

如何在Android中执行Shell命令?

[英]How to execute shell commands in android?

我试图通过我的代码执行Shell命令,以在Iptables中添加条目。 以下是我的一段代码

private void createShellCommand(String uidValue_param, String interface_param) {
    // TODO Auto-generated method stub
    StringBuilder cmdScript=new StringBuilder();
    script.append("iptables -A OUTPUT " + interface_param + "-m owner --uid-owner " + uidValue_param + "-j REJECT");
    writeIptables(cmdScript);

}


private void writeIptables(StringBuilder scriptfile) {
    // TODO Auto-generated method stub
    String cmd=scriptfile.toString();
    if(RootTools.isRootAvailable())
    {
        Process exec;
        try {
            exec = Runtime.getRuntime().exec(new String[]{"su","-c"});
            final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
            out.write(cmd);

            // Terminating the "su" session
            out.write("exit\n");
            out.flush();    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("IPtables updation failed", "Iptable write failed"+e);
        }

    }
    else
    {
        Log.e("Root Access denied", "Access Denied");
    }
}

这里有两种方法,即用于构建shell命令的createshellCommand()和用于更新iptables的writeIptables()。 但是每当我运行程序logcat时,都会显示以下警告

“ W 19913 su请求被拒绝(0-> 0 / system / bin / sh)”

但是我可以通过命令提示符将其手动添加到iptables中,但是使用上面的代码不会更新。 我的手机已扎根,并且我将android 2.3.4与linux内核2.6.29一起使用。 我正在使用外部库“ Roottools”检查根访问权限。

请帮助我纠正错误。

这有效:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    execute_reboot();

}
void execute_reboot()
{
    Process reboot;
    try {
        reboot=Runtime.getRuntime().exec("su");
          DataOutputStream os = 
              new DataOutputStream(reboot.getOutputStream());
            os.writeBytes("reboot\n");
            os.flush();

              os.writeBytes("exit\n");
              os.flush();

            reboot.waitFor();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

此代码工作正常。 您的程序中有几个小错误。 试试我粘贴的那个。 它的工作魅力。 祝一切顺利。 我将其保持尽可能简单,以便于理解。 您仍然可以使用数组和其他东西来修饰代码。

而且,同样的,它也适用于chmod命令,其中您需要传递多个参数。

为此,只需更换

“os.writeBytes(” 重新启动\\ n “);”

“ chmod 777 / dev / video0 \\ n”(或任何其他系统文件)。

谢谢。 让我知道我是否可以做些什么。

public static void rebootNow() {
    Log.d(TAG, "Rebooting");
    try {
        @SuppressWarnings("unused")
        Process proc = Runtime.getRuntime().exec(
                new String[] { "su", "-c", "reboot" });
    } catch (Exception ex) {
        Log.d(TAG, "Rebooting failed (Terminal Error)");
    }
}

这个比较紧凑

您可以添加“ proc.waitFor();” 在Process proc ...行中删除未使用的警告后,但是重新启动设备需要花费几秒钟的时间,如果您想在几秒钟内在UI线程中禁用某些功能,最好不要等待该过程结束。

尝试使用iptables命令(带有sudo和不带sudo),而不是仅仅破坏iptables配置文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM