Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass Data to outputstream

 
SonalPSPL Bagmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse my ignorance, but what's cmd_su, is it any thing we need to know about?
 
SonalPSPL Bagmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Process cmd_su = Runtime.getRuntime().exec("su - imail");
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic