Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide Password for sudo command in java [closed]

Tags:

java

Process p=Runtime.getRuntime().exec("sudo rm -rf /home/ftp");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
bw.write("qwerty");
bw.flush();

I have written this code but it is not working

like image 319
Ankit Singh Avatar asked Jan 02 '17 09:01

Ankit Singh


People also ask

What password should I use for sudo?

There is no default password for sudo . The password that is being asked, is the same password that you set when you installed Ubuntu - the one you use to login. Save this answer.


1 Answers

String[] cmd = {"/bash/bin","-c"," echo password| sudo -S rm -rf /home/ftp"}; 
Process p = Runtime.getRuntime.exec(cmd); 

Provide the input for the process using the pipe. Starting the echo with space it will remove it from bash history.

You can also later delete the history:

new File(System.getProperty("user.home"), ".bash_history").delete();

but be careful with it. There is a trick to remove just last entries.

like image 119
xenteros Avatar answered Oct 25 '22 06:10

xenteros