Passing input into passwd using pipe
Asked Answered
L

3

8

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

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

Laminous answered 23/5, 2014 at 7:10 Comment(2)
Try using expect/auto-expect I guess right tool for you.Timbrel
please don't do this. This command would be stored in the command history. Your password may be seen by all.Faraday
B
25

You need to send the password twice:

(echo 'somepassword'; echo 'somepassword') | passwd someuser
Bosomy answered 23/5, 2014 at 7:18 Comment(0)
M
2

As root you could try

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.

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.

Michaeu answered 23/5, 2014 at 7:22 Comment(0)
G
2

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.

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.

Grochow answered 4/4, 2019 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.