2012-07-09 17 views
0

我开发使用JSchpasswd文件中的一个命令不工作

左右(用于与通过ssh其他机器进行通信的Java库)发送一次性单行命令在不同的Linux机器的工具我们的客户需要更改所有机器上的密码。谷歌帮我达到了这一点:

echo -e "123\n123" | passwd username 其中'123'是新的密码。

该命令执行,但是这始终是输出:

[[email protected] ~]# echo -e "123\n123" | passwd 
Changing password for root 
New password: 
Retype password: 
passwd: password for root is unchanged 

表明该命令没有成功。

请注意,这是一个运行linux的小设备。这是一个私人编译的版本,尽可能紧凑。实际上我对Linux不是很了解!

这是机器的信息:

[[email protected] ~]# uname -a 
Linux QNA-XR1 2.6.22-XR100-v1.1.7 #1 Tue Aug 19 22:55:50 EDT 2008 ppc unknown 

passwd的帮助:

[[email protected] ~]# passwd --help 
BusyBox v1.7.3 (2008-01-09 00:06:30 EST) multi-call binary 

Usage: passwd [OPTION] [name] 

Change a user password. If no name is specified, 
changes the password for the current user. 

Options: 
     -a  Define which algorithm shall be used for the password 
       (choices: des, md5) 
     -d  Delete the password for the specified user account 
     -l  Locks (disables) the specified user account 
     -u  Unlocks (re-enables) the specified user account 

回声帮助

[[email protected] ~]# help echo 
echo: echo [-neE] [arg ...] 
    Output the ARGs. If -n is specified, the trailing newline is 
    suppressed. If the -e option is given, interpretation of the 
    following backslash-escaped characters is turned on: 
     \a  alert (bell) 
     \b  backspace 
     \c  suppress trailing newline 
     \E  escape character 
     \f  form feed 
     \n  new line 
     \r  carriage return 
     \t  horizontal tab 
     \v  vertical tab 
     \\  backslash 
     \num the character whose ASCII code is NUM (octal). 

    You can explicitly turn off the interpretation of the above characters 
    with the -E option. 

非常感谢提前对您有所帮助。

+1

读取密码busybox的代码是非常愚蠢的,并没有做基于行的读数。你应该可以做'(echo 123; sleep 3; echo 123)| passwd' – nos 2012-07-09 20:19:43

+0

@nos,非常感谢你的诀窍。你能否引导我到某个地方去阅读它为什么这样工作? – 2012-07-09 20:31:49

+0

我只是看了一下busybox的密码命令的源代码,这导致了bb_ask_stdin函数读取密码 – nos 2012-07-09 20:32:18

回答

1

/bin/passwd可能会打开/dev/tty强制从终端而不是管道读数。

您可以使用crypt()您的新密码,然后更换密码散列/etc/shadow(对于有它的系统)或/etc/passwd(对于不系统)会更好加密(散列,真的)。这有一些OS依赖的缺点,但它并没有陷入怪异的tty游戏中。

你也许可以强制在ssh中分配一个tty - ssh可以同时使用或不使用它。然后,在以明文形式发送密码两次之前,您会添加一些延迟 - 这种方法不依赖于操作系统,但tty游戏有时可能不那么有趣。

+0

我对Linux不甚了解,但谈到延迟,“nos”的命令表达了你的意思吗?如果你想多解释一下你正在谈论的内容,或者建议阅读一个链接\主题,那将是非常有益的。谢谢。 – 2012-07-10 08:15:11

1

您可以使用chpasswd的(如根,但它并不安全):

# echo "user:passwd" | chpasswd 
+0

我注意到此命令的使用较早,但我们使用的版本没有chpasswd。非常感谢。 – 2012-07-09 20:29:57