我正在尝试编写一个小型 Java 应用程序来覆盖我的/etc/resolv.conf
文件(我使用的是 Ubuntu 12.04)。为此,我需要提供我的root
密码:
myUser@myMachine:~$ sudo vim /etc/resolv.conf
[sudo] password for myUser: *****
因此,执行此操作的过程分为三个步骤:
- Type
sudo vim /etc/resolv.conf
在航站楼
- 终端要求我输入我的
root
密码
- 我输入密码并按
[Enter]
根据我研究的所有内容,我可以使用以下内容来执行上面的步骤 1:
try {
String installTrickledCmd = "sudo vim /etc/resolv.conf";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(installTrickledCmd);
}
catch(Throwable throwable) {
throw new RuntimeException(throwable);
}
但是当执行时,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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)