Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 9 down vote accepted

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:

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

References:

share|improve this answer
    
I just want to give the root password of the remote system in the script. Is it possible in your case? –  karthick87 Jun 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? –  Lekensteyn Jun 3 '11 at 20:23
1  
Bash's read also has an option to disable echoing while reading in the line. See help '\read'. –  geirha Jun 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. –  baldrs Nov 20 '13 at 13:34

To which program do you want to pass a password?

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.

share|improve this answer
    
I just want to pass a password to ssh program. –  karthick87 Jun 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 Diesch Jun 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

share|improve this answer

you can simply do something like this.

# echo -e "password_here\npassword_here" | su user

source

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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