I'm trying to write a small Java app that will overwrite my /etc/resolv.conf
file (I'm on Ubuntu 12.04). To do so, I need to supply my root
password:
myUser@myMachine:~$ sudo vim /etc/resolv.conf
[sudo] password for myUser: *****
So the process for doing this has three steps:
- Type
sudo vim /etc/resolv.conf
at terminal - Terminal asks me to type my
root
password - I enter the password and press
[Enter]
From everything I've researched, I could use the following for performing step #1 above:
try {
String installTrickledCmd = "sudo vim /etc/resolv.conf";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(installTrickledCmd);
}
catch(Throwable throwable) {
throw new RuntimeException(throwable);
}
But when this executes, the shell will want to prompt my Java process for the password. I'm not sure how to wait for this (step #2 above) and then to supply my password back to the shell (step #3 above). Thanks in advance.
Have you tried with -S ?
From man:
Inspired from Viggiano answer.