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

PradyJord
  • 2,136
  • 12
  • 19
user3667832
  • 257
  • 2
  • 5
  • 17

3 Answers3

23

You need to send the password twice:

(echo 'somepassword'; echo 'somepassword') | passwd someuser
Barmar
  • 741,623
  • 53
  • 500
  • 612
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.

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61
1

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.

Chad G
  • 135
  • 8