Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Java向sudo提供根密码?_Java_Linux_Command Line_Sudo - Fatal编程技术网

如何从Java向sudo提供根密码?

如何从Java向sudo提供根密码?,java,linux,command-line,sudo,Java,Linux,Command Line,Sudo,我正在尝试编写一个小型Java应用程序,它将覆盖我的/etc/resolv.conf文件(我在Ubuntu 12.04上)。为此,我需要提供我的root密码: myUser@myMachine:~$ sudo vim /etc/resolv.conf [sudo] password for myUser: ***** 因此,这一过程有三个步骤: 在终端输入sudo vim/etc/resolv.conf 终端要求我键入我的root密码 我输入密码并按[输入] 根据我所做的所有研究,我可以使用

我正在尝试编写一个小型Java应用程序,它将覆盖我的
/etc/resolv.conf
文件(我在Ubuntu 12.04上)。为此,我需要提供我的
root
密码:

myUser@myMachine:~$ sudo vim /etc/resolv.conf 
[sudo] password for myUser: *****
因此,这一过程有三个步骤:

  • 在终端输入
    sudo vim/etc/resolv.conf
  • 终端要求我键入我的
    root
    密码
  • 我输入密码并按
    [输入]
  • 根据我所做的所有研究,我可以使用以下方法来执行上面的步骤#1:


    但是当这个命令执行时,shell会提示我的Java进程输入密码。我不确定如何等待(上面的第2步),然后将密码提供回shell(上面的第3步)。提前谢谢。

    您试过使用-S吗

    $echo mypassword | sudo -S vim /etc/resolv.conf
    
    来自人类:

    The -S (stdin) option causes sudo to read the password from the standard input 
    instead of the terminal device.  The password must be followed by a newline 
    character.
    

    灵感来源于Viggiano的回答。

    我认为这个解决方案应该有效:而不是
    echo
    -你也可以使用密码,然后将密码写入该流。我知道这是旧的,而且只是一个示例,但我想警告一下,将密码放入字符串中是不好的做法。这将导致密码无限期地保留在内存中。在您的例子中,因为您使用了一个文本,所以它在JVM的生命周期中一直保存在内存中。您应该在内存中尽可能短的时间内保存未加密的密码,并且应该尽可能短暂地存储它——通常,char[]是首选。@JakeRobb如何从JVM中找到密码。
    The -S (stdin) option causes sudo to read the password from the standard input 
    instead of the terminal device.  The password must be followed by a newline 
    character.
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    
    public class Test1 {
    
      public static void main(String[] args) throws Exception {
        String[] cmd = {"sudo","-S", "ls"};
        System.out.println(runSudoCommand(cmd));
      }
    
      private static int runSudoCommand(String[] command) throws Exception {
    
        Runtime runtime =Runtime.getRuntime();
        Process process = runtime.exec(command);
        OutputStream os = process.getOutputStream();
        os.write("harekrishna\n".getBytes());
        os.flush();
        os.close();
        process.waitFor();
        String output = readFile(process.getInputStream());
        if (output != null && !output.isEmpty()) {
          System.out.println(output);
        }
        String error = readFile(process.getErrorStream());
        if (error != null && !error.isEmpty()) {
          System.out.println(error);
        }
        return process.exitValue();
      }
    
      private static String readFile(InputStream inputStream) throws Exception {
        if (inputStream == null) {
          return "";
        }
        StringBuilder sb = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
          bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
          String line = bufferedReader.readLine();
          while (line != null) {
            sb.append(line);
            line = bufferedReader.readLine();
          }
          return sb.toString();
        } finally {
          if (bufferedReader != null) {
            bufferedReader.close();
          }
        }
      }
    
    }