在 Java 中更改 SAMBA 密码

Changing SAMBA Password in Java

我在 linux 上有一个 Java 程序 运行 需要能够设置用户初始 samba 密码,然后允许他们更改密码而不给他们访问终端。

下面是我更改用户密码的代码,因为这更容易测试,一旦我解决了这个问题,我就能弄清楚另一部分。

-s 标志应该允许使用标准输入。

String cmd = "smbpasswd -s -U user";
Process p = Runtime.getRuntime().exec(cmd);
OutputStreamWriter Out = new OutputStreamWriter(p.getOutputStream());
InputStreamReader In = new InputStreamReader(p.getInputStream());
BufferedWriter Write = new BufferedWriter(Out);
BufferedReader Read = new BufferedReader(In);
char[] output = null;

//I write all of the output lines to the log, but nothing is written, and the password doesn't change.
Read.read(output);
Write.write(OldPass);
Read.read(Output);
Write.write(NewPass);
Read.read(Output);
Write.write(NewPass);
Read.read(Output);

我需要一些帮助来找出我做错了什么,以及我将如何正确地解决这个问题。感谢任何帮助。

根据 SMBPASSWD(8) 的手册页:

-s: This option causes smbpasswd to be silent (i.e. not issue prompts) and to read its old and new passwords from standard input, rather than from /dev/tty (like the passwd(1) program does). This option is to aid people writing scripts to drive smbpasswd

强调"not issue prompts"。如果我正确阅读了您的代码,您似乎正在等待实用程序的提示,但不会出现(从命令行测试)。但我可能误解了您的 Java 代码。