Long time i am searching for this, i would like to know is it possible to pass passwords in a shell script? Many of the answers returned with no. Recently i read an article stating that how to pass passwords in a shell script. I have tried that, but it doesn't seem to work. This is the link. Can anyone checkitout and revert back? Also pls say me is there a way to pass passwords in a shell script? If no pls say me how linux gets the input for the password?

4 Answers
4

By "entering passwords", you likely mean entering data without being visible for the user.

(suggested by geirha) When using bash, you can use the -s option to prevent typed characters from being displayed:

read -p "Password please: " -s pass

Alternatively, change the behavior of the terminal to hide typed characters with stty -echo (disable echo). After reading the password with the shell built-in read into a variable (in the below example, $pass), turn it back on with stty echo. Because the new line from Enter is hidden to, you've to print a newline to get future output on a new line.

stty -echo
read -p "Password please: " pass
stty echo
printf '\n'

read and printf are shell built-ins. stty is provided by the coreutils package which is installed by default. That means that this snippet is very portable.

Note: the -p option is not standard, but from bash. If you need to display a prompt in other shells, use:

I just want to give the root password of the remote system in the script. Is it possible in your case?
–
karthick87Jun 3 '11 at 19:58

@karthick87: no, this script just captures the data the user types in interactively. If you want to put root passwords in scripts, why don't you consider keybased logins?
–
LekensteynJun 3 '11 at 20:23

1

Bash's read also has an option to disable echoing while reading in the line. See help '\read'.
–
geirhaJun 5 '11 at 12:08

In zsh it is achievable through read -s 'pw?Prompt text', where pw is a variable where password will be placed and -s is a flag to hide input.
–
baldrsNov 20 '13 at 13:34

The script on the link works for me. Note that is not a shell script but an expect script (needs the package expect to be installed`). Using expect is a common way to automate text based interactive programs.

Non-interactive ssh logins are often done using key-based authentication with an empty passphrase.

Some other programs (like sudo) have options to read a password from stdin.

Providing a password as a command line option is often a security problem as on most systems any user can see any other users processes including there command line arguments using simple tools like ps.

I just want to pass a password to ssh program.
–
karthick87Jun 3 '11 at 19:59

So maybe key authentication works for you: Use ssh-keygen to create a authentication key (just press Enter when asked for a pass phrase) and copy it to the remote user using ssh-copy-id.If key authentication is enabled for sshd (it is by default) ssh will then not ask you for a password.
–
Florian DieschJun 3 '11 at 21:13

A script should never really handle passwords. Have whichever application needs the password ask for it itself, or if that's not possible, find a better means of authenticating with the application. Please read http://mywiki.wooledge.org/BashFAQ/069