wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pass Data to outputstream" Watch "Pass Data to outputstream" New topic
Author

Pass Data to outputstream

SonalPSPL Bagmar
Greenhorn

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";

stdOut.write(pswd.getBytes());
stdOut.flush();
//----------------------------------------------------------------------

Thanks
SONAL
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
Greenhorn

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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]
 
 
subject: Pass Data to outputstream
 
Similar Threads
can anyone explain the flow of the code
write to a file
how to execute commands from java
Logging in to a site programmatically
Communicate with the server without a browser.