64

I am creating my own bash script, but I am stuck at the moment. Basically, the script would be used to automate server setup in CentOS. Some software normally asks the user to type a password. I want the script to put the password that I have generated and stored as a variable instead of asking the user.

When the message "New password:" appears during install, how can I make the script put the value stored in a variable $key as if the user had typed it, with a bash script?

1

2 Answers 2

67

You should find the 'expect' command will do what you need it to do. It's widely available.

A very rough example:

#!/usr/bin/expect
set pass "mysecret"

spawn /usr/bin/passwd

expect "password: "
send "$pass"
expect "password: "
send "$pass"
Sign up to request clarification or add additional context in comments.

6 Comments

dude here is the "New password:" appears while the script is installing stuff with yum(e.g like mysql}.Could you plz show me a code snippet for this particular case.I use sed mostly and never used expect so far.From tutorial you provided , its seems you can use expect when user is suppose to enter something
Look at the #! line - this is an Expect script. The whole thing is run by expect, not by bash, so it's written in the Expect language (which is basically Tcl). The important commands are there - spawn to start the command you're interacting with, expect to wait for it to send a specific string, and send to send a reply. If you want to turn control back over to the user, you can do so with interact.
what do you do if you don't have spawn, expect, or send ?? how do we do this with tools we don't have to install?
@user1747935 if you have expect command, then you have everything you need. Notice the hashbang #!/usr/bin/expect - you aren't supposed to run this script from bash shell.
@user1747935: here's a superuser answer on how to avoid expect scripts: (echo "multiple"; echo "inputs to" ) | your_executable_script.sh
|
4

Here is a snippet I wrote; to ask for users' password and set it in /etc/passwd. You can manipulate it a little probably to get what you need:

echo -n " Please enter the password for the given user: "
read userPass
useradd $userAcct && echo -e "$userPass\n$userPass\n" | passwd $userAcct > /dev/null 2>&1 && echo " User account has been created." || echo " ERR -- User account creation failed!"

4 Comments

You guys are gettting me wrong.I don't want to read user input.All i want to do is when (yum install something) requires user input for password , it should place the value found in variable $key as input instead of asking user to input something
Like when "New Password:" prompt appears during install, then set value in $key as pass
in that, only expect will do the job. for instance, I used expect to pass a value when an external program asks for input from user. like when i want to add my ssh key to my ssh-agent, i automatically pass the pass-phrase to ssh-add via expect.
Thank you....also does expect already "stimulate" enter key press after it set the pass or i have to create another command for enter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.