• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to provide password to the prompt through Java

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic