Howto: Change users password in Linux with Java

Hi, how can i write a Java program to change user acounts passwords in linux?
[84 byte] By [MarshallAVT] at [2007-9-30 17:39:09]
# 1
The easiest way is probably running passwd in an xterm with Runtime.exec.
jsalonen at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...
# 2

something like code below is not working:

has anyone here ever executed passwd this way successfully?

String[] callAndArgs = { "passwd",

"ehsan" };

try {

String line = null;

Process child = rt.exec(callAndArgs);

System.out.println("111");

BufferedReader br = new BufferedReader( new InputStreamReader(

child.getInputStream() ) );

BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(

child.getOutputStream() ) );

System.out.println(br.readLine() );

bw.write( "password" );

System.out.println(br.readLine() );

bw.write( "password" );

System.out.println( "Data : " + br.readLine() );

System.out.println( "DEBUGGER" );

child.destroy();

br.close();

bw.close();

System.out.println("Process exit code is: " + child.exitValue());

}

catch(IOException e) {

System.err.println(

"IOException starting process!");

}

MarshallAVT at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...
# 3

Post code using the formatting tags...

http://forum.java.sun.com/features.jsp#Formatting

> something like code below is not working:

HOW is it not working?

> has anyone here ever executed passwd this way

> successfully?

Many people have successfully used Runtime.exec() to run console applications.

jschell at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...
# 4

My problem is in executing passwd, and im asking if anyone has ever written a program in java to change user acounts passwords using passwd command

I cant get the output of "passwd"command and I cant send any streams to the command

String[] callAndArgs = { "passwd",

"poli" };

Process child = rt.exec(callAndArgs);

InputStream stdin = child.getInputStream();

InputStreamReader isr = new InputStreamReader(stdin);

BufferedReader br = new BufferedReader(isr);

InputStream stderr = child.getErrorStream();

InputStreamReader isr2 = new InputStreamReader(stderr);

BufferedReader br2 = new BufferedReader(isr2);

while ( (line = br.readLine()) != null) // not working -- program halts here

System.out.println(line); // not working

OutputStream os = child.getOutputStream();

PrintWriter pw = new PrintWriter(os);

pw.println ("password");// not working

pw.flush();

MarshallAVT at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...
# 5

I am not sure but I think the passwd command does not use the standard input or output streams to communicate with the user. Rather it reads and writes directly to the console. But Runtime.exec does not create a console, so passwd cannot be used this way. This is why I suggested exec'ing an xterm to run passwd. You might have better luck with this (I don't have graphical access to a unix box ATM so can't test it myself):

String[] callAndArgs = {

"xterm",

"-e",

"passwd",

"poli"};

Process child = rt.exec(callAndArgs);

jsalonen at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...
# 6

> My problem is in executing passwd, and im

> asking if anyone has ever written a program in java to

> change user acounts passwords using passwd command

>

> I cant get the output of "passwd"command and I cant

> send any streams to the command

Searching the forums suggest that, as the previous reply suggests, that command does not use stdin/out (nor does su.)

Suggested solutions were to create a script file or to use JNI. I didn't see anything that suggested those would work but rather that they might.

jschell at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...
# 7

this is how i solved the problem:

Runtime rt = Runtime.getRuntime();

String[] callAndArgs = { "passwd",

"--stdin",

"ehi" };

try {

String line = null;

Process child = rt.exec(callAndArgs);

InputStream stdin = child.getInputStream();

InputStreamReader isr = new InputStreamReader(stdin);

BufferedReader br = new BufferedReader(isr);

OutputStream os = child.getOutputStream();

PrintWriter pw = new PrintWriter(os);

pw.println ("empty");

pw.flush();

if ((line = br.readLine()) != null)

System.out.println(line);

pw.println("password2");

pw.flush();

child.waitFor();

System.out.println("Process exit code is: " + child.exitValue());

}

catch(IOException e) {

System.err.println(

"IOException starting process!");

} catch (InterruptedException e) {

e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

}

MarshallAVT at 2007-7-6 14:07:44 > top of Java-index,Administration Tools,Sun Connection...