显示Shell脚本的进程

发布于 2024-11-07 13:57:31 字数 703 浏览 0 评论 0原文

我有以下方法在我的 java 应用程序中运行 shell 命令,并且我希望运行一些脚本,例如修复用户手机上所有应用程序权限的脚本。我可以使用此命令 execCommand("/system/xbin/fix_perm"); 毫无问题地运行脚本但问题是我只想打印出正在执行的操作,就像终端模拟器一样,我如何获取输出流并将其打印在屏幕上?感谢您的帮助

public Boolean execCommand(String command) 
{
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
        os.writeBytes(command + "\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true;
}

I have the following method for running shell commands in my java applications and i'm looking to run a few scripts such as one that fixes permissions for all applications on the users phone. I can run the script no problem by using this command execCommand("/system/xbin/fix_perm"); however the problem is that i want to just print out what's being done like terminal emulator does how can i take my outputstream and print it on the screen? Thank you for any help

public Boolean execCommand(String command) 
{
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
        os.writeBytes(command + "\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2024-11-14 13:57:31

我希望您了解允许用户以 su 形式运行任意命令可能带来的极端后果,并指出可能的解决方案。

public Boolean execCommand(String command) 
{
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");

        // capture stdout
        BufferedReader stdout = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
        // capture stderr
        BufferedReader stderr = new BufferedReader(
            new InputStreamReader(process.getErrorStream()));

        DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
        os.writeBytes(command + "\n");
        os.flush();

        String line = null;
        StringBuilder cmdOut = new StringBuilder();
        while ((line = stdout.readLine()) != null) {
            cmdOut.append(line);
        }
        stdout.close();
        while ((line = stderr.readLine()) != null) {
            cmdOut.append("[ERROR] ").append(line);
        }
        stderr.close();

        // Show simple dialog
        Toast.makeText(getApplicationContext(), cmdOut.toString(), Toast.LENGTH_LONG).show();

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

        // consider dropping this, see http://kylecartmell.com/?p=9
        process.waitFor(); 
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true;
}

I'm hoping you're aware of the potentially extreme consequences of allowing a user to run arbitrary commands as su and will instead point to a possible solution.

public Boolean execCommand(String command) 
{
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");

        // capture stdout
        BufferedReader stdout = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
        // capture stderr
        BufferedReader stderr = new BufferedReader(
            new InputStreamReader(process.getErrorStream()));

        DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
        os.writeBytes(command + "\n");
        os.flush();

        String line = null;
        StringBuilder cmdOut = new StringBuilder();
        while ((line = stdout.readLine()) != null) {
            cmdOut.append(line);
        }
        stdout.close();
        while ((line = stderr.readLine()) != null) {
            cmdOut.append("[ERROR] ").append(line);
        }
        stderr.close();

        // Show simple dialog
        Toast.makeText(getApplicationContext(), cmdOut.toString(), Toast.LENGTH_LONG).show();

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

        // consider dropping this, see http://kylecartmell.com/?p=9
        process.waitFor(); 
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文