Java 获取 bash root 访问权限

标签 java linux root sudo

我正在尝试在 Linux 上为我正在制作的程序安装缺少的依赖项。然而,我无法获得安装缺少的依赖项所需的根访问权限。这是我到目前为止所拥有的:

我的逻辑如下:

1) 检查是否使用 pacapt 安装了依赖项(本例中为 npm) 2)如果是这样,则使用文本提示获取用户密码 3)然后继续进一步的指令,如下所示:echo [userpass] | sudo -S ...

现在'echo [userpass] | sudo -S ...' 命令像这样打印到 shell 中; [用户密码] | sudo -S ...(其中显示用户密码代替 [userpass]),但不执行。

这是我的代码:

public class LinuxDependencyCheck extends Application{
    public static void main (String [] args){
            launch(args);
    }

    @Override
    public void start(Stage mainWindow){

            String userPass = null;
            String terminalOut = null;

            terminalOut = runBash("./LinuxScripts/pacapt -Qqe npm");

            if (terminalOut.equals("npm")){
                    userPass = getUserPass();
                    if (userPass != null){
                            System.out.println("runing");
                            runBash("echo " + userPass + " | sudo -S npm install" + 
                                            " phantomjs2");
                    }
            }

    }

    public String runBash(String runCommand){
            String result = null;
            String returnVal = null;
            try {
                    Runtime r = Runtime.getRuntime();                    

                    Process p = r.exec(runCommand);

                    BufferedReader in =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                            System.out.println(inputLine);
                            result += inputLine;
                            returnVal = inputLine;
                    }
                    in.close();

            } catch (IOException e) {
                    System.out.println(e);
            }

            return returnVal;
    }

    public String getUserPass(){
            TextInputDialog dialog = new TextInputDialog("Password");
            dialog.setTitle("Installation helper");
            dialog.setHeaderText("It looks like you are missing" + 
                            " dependecies to complete this action" + 
                            " would you like to try to install" +
                            " them now");
            dialog.setContentText("Please enter your password :");

            // Traditional way to get the response value.
            Optional<String> result = dialog.showAndWait();
            if (result.isPresent()){
                    return result.get().toString();
            }
            return result.get();
    }
}

最佳答案

您的 runBash() 方法命名不当,因为它不会导致通过 bash 运行给定的命令。因此,它也不适合与您指定的命令字符串一起使用,它依赖于 shell 的管道运算符将两个单独的命令串在一起。

当你这样做时......

runBash("echo " + userPass + " | sudo -S npm install" + 
                                            " phantomjs2");

... Java 在空格上分割字符串,将第一个子字符串(“echo”)作为命令,并使用所有其他子字符串作为参数来执行该命令。不用说,它运行时不会出错,但也不会达到您想要的效果。

如果您确实想通过 bash 执行命令字符串(正如您所看到的那样),那么在您的 runBash() 方法中您可以更改此...

                Process p = r.exec(runCommand);

...对此...

                Process p = r.exec("/bin/bash", "-c", runCommand);

。这至少应该能让你克服第一个障碍。

您还应该关闭ProcessOutputStream(通过它您可以将数据通过管道传输到进程中),并排出Process的错误流。一般来说,您需要并行排出输入流和错误流,因为如果其中一个缓冲区已满,则进程可能会阻塞。也许对于这个特定的命令来说这不是一个风险,但你需要做出判断。 waitFor() Process 也是一种很好的形式;这样做可以避免 Java 累积僵尸子进程。

关于Java 获取 bash root 访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34685538/

相关文章:

java - 从文件 Java 8 中读取行并在两者之间中断的最佳方法

java - 在jframe resizeize上调整cubiccurve的大小

java - 读取文件,并使用Word类、方法处理单词

php - 如何限制命令行应用程序可以在 Linux Web 服务器上使用的资源

linux - Bash - 递归更改仅属于特定用户/组的目录的所有权

linux - 在 "find -exec"中使用 eval 时出现意外行为

linux - cd 到 root - 在 Matlab 脚本中不起作用

java - Swing:从另一个类获取组件的父级

node.js - gyp WARN EACCES 用户 "root"没有访问开发目录的权限

android - 将文件从内部文件夹复制/移动到 SD 卡的应用程序?临时根?