aspose file tools*
The moose likes Beginning Java and the fly likes How to provide password to the prompt through Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to provide password to the prompt through Java" Watch "How to provide password to the prompt through Java" New topic
Author

How to provide password to the prompt through Java

jaibhim max
Greenhorn

Joined: Nov 15, 2010
Posts: 21
Hi All,

I am using Ubuntu machine with JDK 6. I am running following command on shell-
. Now, it prompts for sudo password and I provide the sudo password on shell and "somescript" starts running with sudo permissions.

What I want to do is, I need to execute "sudo ./somescript" fromJava code. For that I have created a jar file which executes following command
now, the problem is I dont get any prompt where I can provide the sudo password.

Please guide me to provide password to sudo.


-akash

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

See if you can use Apache Commons Exec API to help you with this. They provide a wrapper around for executing external programs from Java.


Mohamed Sanaulla | My Blog
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

jaibhim max wrote:Hi All,
Please guide me to provide password to sudo.


I suspect you are not going to like the answer!

The password for 'sudo' needs to be presented thought the keyboard or it needs to be presented though a process defined by the SUDO_ASKPASS environment variable using "sudo -A". By invoking your script through Java your script won't get access to the keyboard so you must set the environment variable to point to a program that returns the password terminated by a "\n". Rather than use 'sudo' directly you can use 'gksudo' which will bring up a dialogue prompting the user for the password. This is my preferred solution.

Don't even think about creating a script that returns the hard coded password using echo. This is insecure. There are ways to make this more secure but it means that your password would need to be either stored in your Java program or you would have to create your own prompt.

"man sudo" spells out most of the details.

P.S. I do hope you have read the 4 sections of http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.htm and implemented ALL the recommendations.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
reddy ajay
Greenhorn

Joined: Apr 21, 2011
Posts: 8
Hi, I struggled with the same issue for hours and finally got the solution.
HERE it is. Brace yourself:
echo "password"|sudo -S installer -pkg /YOURDIRECTORY -target /

"password" would be your password....you can give this in one line in java as: Runtime.getRuntime().exec(s);
hope this helped and let me know if it doesnt work because it worked for me and i can help you out
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

geek mr wrote:HERE it is. Brace yourself:
echo "password"|sudo -S installer -pkg /YOURDIRECTORY -target /

This is exactly what James warned against because it is insecure:

James Sabre wrote:Don't even think about creating a script that returns the hard coded password using echo. This is insecure.

So, don't do this!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

It also won't work since the | is a shell tool, and won't be treated as such by Runtime.exec that only can handle single processes, not combined processes like these.

You may be able to use process.getInputStream() and write to that, but I'm not sure.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Rob Spoor wrote:It also won't work since the | is a shell tool, and won't be treated as such by Runtime.exec that only can handle single processes, not combined processes like these.

You may be able to use process.getInputStream() and write to that, but I'm not sure.


And even if you get the shell involved by using


it doesn't work in Ubuntu 11.04 or 10.10 since one is still prompted for the password. The problem is that the password has to entered through the 'keyboard' device and not through 'stdin'.

I would be interested to know details of the system "geek mr" used to test this.
reddy ajay
Greenhorn

Joined: Apr 21, 2011
Posts: 8
James Sabre wrote:
Rob Spoor wrote:It also won't work since the | is a shell tool, and won't be treated as such by Runtime.exec that only can handle single processes, not combined processes like these.

You may be able to use process.getInputStream() and write to that, but I'm not sure.


And even if you get the shell involved by using


it doesn't work in Ubuntu 11.04 or 10.10 since one is still prompted for the password. The problem is that the password has to entered through the 'keyboard' device and not through 'stdin'.

I would be interested to know details of the system "geek mr" used to test this.


I ran this on my mac OS in the command line interface and it ran just fine......I dont know why you guys have trouble running it. I guess, You have to get the shell involved using bash -c when trying to run from java code......just give this command "echo password |sudo -S installer -pkg /YOURDIRECTORY -target / on your command line interface in mac OS....and its gonna run fine.
I understand its not secure but there is no other way to get your work done. So this might well be it.
reddy ajay
Greenhorn

Joined: Apr 21, 2011
Posts: 8
geek mr wrote:
James Sabre wrote:
Rob Spoor wrote:It also won't work since the | is a shell tool, and won't be treated as such by Runtime.exec that only can handle single processes, not combined processes like these.

You may be able to use process.getInputStream() and write to that, but I'm not sure.


And even if you get the shell involved by using


it doesn't work in Ubuntu 11.04 or 10.10 since one is still prompted for the password. The problem is that the password has to entered through the 'keyboard' device and not through 'stdin'.

I would be interested to know details of the system "geek mr" used to test this.


I ran this on my mac OS in the command line interface and it ran just fine......I dont know why you guys have trouble running it. I guess, You have to get the shell involved using bash -c when trying to run from java code......just give this command "echo password |sudo -S installer -pkg /YOURDIRECTORY -target / on your command line interface in mac OS....and its gonna run fine.
I understand its not secure but there is no other way to get your work done. So this might well be it.


I have worked with the whole thing now and it worked perfectly fine when i ran it from my java code....all i had to do was to invoke shell by using
String[] cmd= {"/bin/bash", "-c", "echo password |sudo -S installer -pkg /YOURDIRECTORY -target /"};
Runtime.getRuntime.exec(cmd);

PEACE OUT!!!
reddy ajay
Greenhorn

Joined: Apr 21, 2011
Posts: 8
Rob Spoor wrote:It also won't work since the | is a shell tool, and won't be treated as such by Runtime.exec that only can handle single processes, not combined processes like these.

You may be able to use process.getInputStream() and write to that, but I'm not sure.


use :
String[] cmd = {"/bash/bin","-c","echo password| sudo -S installer -pkg /yourdirectory -target"};
Runtime.getRuntime.exec(cmd);

Works smooth.....I totally nailed it.
PEACE!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to provide password to the prompt through Java