1

I would like to programmatically answer a password prompt using only shell script when prompted. Is it possible to automate this task in this way? I want to avoid other tools entirely - so no expect or similar - I would like to achieve this simply using shell script.

4

Since you do not specify which shell, I'll just cheat. You can find the following and much else on grml.org's zsh-lover's command reference man page.

  $ zmodload zsh/zpty
  $ zpty PW passwd $1
  $ zpty PW passwd $1
# ``-r'': read the output of the command name.
# ``z'' : Parameter
  $ zpty -r PW z '*password:'
# send the to command name the given strings as input
  $ zpty -w PW $2
  $ zpty -r PW z '*password:'
  $ zpty -w PW $2
# The second form, with the -d option, is used to delete commands
# previously started, by supplying a list of their names. If no names
# are given, all commands are deleted. Deleting a command causes the HUP
# signal to be sent to the corresponding process.
  $ zpty -d PW

I even managed to do it myself. Here's a copy+paste from my terminal:

mikeserv@localhost ~ % :                  
sudo zsh -c '
zmodload zsh/zpty
userdel dummy
useradd -m dummy
zpty dummyadd "passwd dummy"
zpty -r dummyadd nl "*UNIX password:"
zpty -w dummyadd "dummypw^M"
zpty -r dummyadd nl "*UNIX password:"
zpty -w dummyadd "dummypw^M"
zpty -r dummyadd nl "*"
printf %s\\n "$nl"
zpty -d dummyadd
'
userdel: user 'dummy' does not exist
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.

mikeserv@localhost ~ % ssh dummy@localhost
dummy@localhost's password: 
dummy@localhost ~ % 

It took me a few tries - that's why dummy's home directory was already there - but you can see that when I ran that command that user did not exist, and I scripted a session for setting dummy's password. It's pretty clumsy script granted, but, when it was over, I was able to login as dummy.

So I guess it's possible. Maybe not advisable, but maybe that depends on what you're about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.