bash 使用管道将输入传递到 passwd

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23823089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:31:16  来源:igfitidea点击:

Passing input into passwd using pipe

linuxbashpipepasswd

提问by user3667832

How can I pipe some input using echo, into program that requires user typing something two times?

如何使用echo, 将某些输入管道传输到需要用户输入两次内容的程序中?

for example

例如

echo "somepassword"|passwd someuser

creates this error message

创建此错误消息

Enter new UNIX password: Retype new UNIX password: passwd: Authentication token manipulation error
passwd: password unchanged

because I didn't retyped password

因为我没有重新输入密码

回答by Barmar

You need to send the password twice:

您需要发送两次密码:

(echo 'somepassword'; echo 'somepassword') | passwd someuser

回答by user3667832

As root you could try

作为 root,你可以尝试

echo "somepasswored" | passwd someuser --stdin

--stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.

--stdin 该选项用于指示 passwd 应该从标准输入中读取新密码,标准输入可以是管道。

If you're not root then you should't be able to change some other users password. If you really want to go through the whole business of providing 2 passwords the expect is a good tool to use.

如果您不是 root,那么您应该无法更改其他一些用户的密码。如果你真的想完成提供 2 个密码的整个业务,expect 是一个很好的工具。

回答by Chad G

Can also do this

也可以这样做

echo -e "currentPassword\nNewPassword\nNewPassword" | passwd

the -e will make echo actually use a new line, not just print \n This option is for when you need the current password before your new one if the current password isnt needed, you can omit it.

-e 将使 echo 实际上使用一个新行,而不仅仅是打印 \n 此选项用于当您在新密码之前需要当前密码时,如果不需要当前密码,则可以省略它。

echo -e "NewPassword\nNewPassword" | passwd

I also agree that this is bad practice for multiuse/public computers, but if you know what your doing, and have a good reason for it, this works.

我也同意这对于多用途/公共计算机来说是不好的做法,但是如果您知道自己在做什么,并且有充分的理由,那么这很有效。