Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give password in shell script?

In a shell script file I am using some commands like scp and make install which ask for my password.

I run a shell script to compile a big project, and after some time it asks for my password for using scp. I need to wait for that process and give the password after that.

I just want to do it all by shell script without interaction, so how can I avoid being prompted for the password here?

like image 287
Jeegar Patel Avatar asked Feb 20 '12 09:02

Jeegar Patel


3 Answers

Short answer: DON'T

Use public key authentication for SCP and sudo with NOPASSWD directive for make install

like image 192
Kimvais Avatar answered Sep 27 '22 17:09

Kimvais


If you can't use ssh trust and must enter the password later on in your script, use read -s -p "Password:" USER_PASSWORD to silently read in the password. You can then export USER_PASSWORD to an expect script, avoiding it being displayed in ps:

    #!/usr/bin/expect -f
    spawn scp some.file USER@otherhost:~
    expect "assword:"
    send -- "$env(USER_PASSWORD)\r"
    expect eof
like image 23
Alex Avatar answered Sep 27 '22 16:09

Alex


I think it's a better idea to generate an authentication key, and use this key based authentication instead of writing plain text passwords into your scripts.

like image 39
KARASZI István Avatar answered Sep 27 '22 16:09

KARASZI István