File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Pass Data to outputstream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pass Data to outputstream" Watch "Pass Data to outputstream" New topic

Pass Data to outputstream

SonalPSPL Bagmar

Joined: Jul 29, 2004
Posts: 22
Hi all,
i want to pass password to a unix command. is there anything wrong with the following code snippet....??its not writing the password there...

BufferedReader stdInput = new BufferedReader(new InputStreamReader(cmd_su.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(cmd_su.getErrorStream()));
OutputStream stdOut = cmd_su.getOutputStream();
String pswd = "mypwd";


Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Excuse my ignorance, but what's cmd_su, is it any thing we need to know about?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
SonalPSPL Bagmar

Joined: Jul 29, 2004
Posts: 22
Process cmd_su = Runtime.getRuntime().exec("su - imail");
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Commands like "su", "passwd" and the like generally don't read passwords from standard input: they open the terminal device and read directly from there. This is how they manage to not echo the password to the terminal when you type it in.

It's possible to execute such a program from another program, but only by using ptys directly, not something you can do in pure Java. You could write an external program in C to do it, or use native methods.

Another easier idea for you: can you put the commands you need to run into a shell script and make it setuid to "imail?" This would avoid the need for a password altogether.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Pass Data to outputstream
It's not a secret anymore!