简体   繁体   English

在Java中更改SAMBA密码

[英]Changing SAMBA Password in Java

I have a Java program running on linux that needs to be able to both set a users initial samba password, and then allow them to change their password without giving them access to the terminal. 我有一个在Linux上运行的Java程序,它需要能够设置用户的初始samba密码,然后允许他们更改其密码而不授予他们访问终端的权限。

Below is my code for changing the users password, as this is easier to test with, and I will be able to figure the other part out once I get this worked out. 下面是我用于更改用户密码的代码,因为它更易于测试,一旦解决,我将能够弄清楚另一部分。

The -s flag is supposed to allow stdin to be used. -s标志应该允许使用stdin。

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

I need some help to figure out what I am doing wrong, and how I would go about this correctly. 我需要一些帮助来弄清楚我在做错什么,以及如何正确解决此问题。 Any help is appreciated. 任何帮助表示赞赏。

According to the man page for SMBPASSWD(8) : 根据SMBPASSWD(8)的手册页:

-s: This option causes smbpasswd to be silent (ie 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). -s:此选项使smbpasswd 保持沉默(即不发出提示) ,并从标准输入而不是从/ dev / tty中读取其旧密码和新密码(就像passwd(1)程序一样)。 This option is to aid people writing scripts to drive smbpasswd 此选项是为了帮助人们编写脚本来驱动smbpasswd

Emphasis on "not issue prompts". 强调“不发出提示”。 If I'm reading your code correctly, you seem to be waiting for prompts from the utility, which won't come (test it from the command line). 如果我正确地阅读了您的代码,您似乎正在等待实用程序的提示,而不会出现(从命令行对其进行测试)。 But I may have misinterpreted your Java code. 但是我可能误解了您的Java代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM