简体   繁体   中英

Passing password during git clone from shell script

I have written a shell script to clone the project from multiple location which is sent as a file. I read the file and pass the each location in a loop and perform my operation.

LocationFile.txt

Project1=https://enduser1@stash.my.org/my/path/testProject1.git
Project2=https://enduser2@stash.my.org/my/path/testProject2.git
Project3=https://enduser3@stash.my.org/my/path/testProject3.git
Project4=https://enduser4@stash.my.org/my/path/testProject3.git

Password.txt

Project1=password1
Project2=password2
Project3=password3
Project4=password4

I have a password file encrypted and stored in server, which I can read as part of script and passing into my script When I run my script during clone step its prompting for a password, I tried to pass the password like below which is not working

##############
gitpassword=$password1 #retrieved from Password.txt

for xxx
    do
    ...
    ... 
    git clone "${ProjectArrayList[1]}" 
    read $gitpassword
    ...
    ..
    done
##############

I know I can do password less if I use ssh, which I don't want to do. Is there a way to achieve this with my exsiting approach

You can use the expect and send functions to simulate user input automatically.

Here is what the syntax would be like:

expect -c 'spawn sudo git clone "${ProjectArrayList[1]}"; expect assword; send "password\n"; interact'

Git has a "credential helper" that can do the job [it is designed to work over https].

See: https://git-scm.com/docs/git-credential-store and https://help.github.com/articles/caching-your-github-password-in-git/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM