Provide passphrase to git in bash script
Asked Answered
I

1

10

How do I provide passphrase with git fetch/pull in bash script. I really need to do it in a bash script, without using ssh-add or something like that. is it possible?

Intact answered 8/12, 2015 at 11:58 Comment(6)
Well, it is my belief that git supports HTTP Basic Auth. So if you're happy with it (wouldn't recommend), just authenticate over HTTPS with a URL of the form https://username:password@hostname/path/to/foo.git.Bridgetbridgetown
You can also use an empty passphrase to avoid the passphrase prompt - again not recommended as insecure.Mystic
I think the best way to store key passphrase is using memory. Why don't you want to use ssh agent?Igor
Not sure whether I understood your question correctly. Is it that the access to your repo is done via ssh, but you don't want to enter the password every time? In this case, you could set the environment variable SSH_ASKPASS to a shell script which writes the password to standard out - provided that you don't mind the security risk which comes with this.Colic
If you don't mind to expose your password in this way, there are better alternatives to SSH_ASKPASS.Colic
I mean ssh-key passphrase when I try to fetch repository or pull it. It's not a password for repository. Is there way to give it to git pull/fetch when it asking for passphrase in bash script?Intact
I
9

I tryed ssh-agent and solution with SSH_ASKPASS but nothing worked, then I found a solution using http://expect.sourceforge.net/

Example(executed in shell):

pass="passwod"
/usr/bin/expect <<EOD
spawn git fetch origin $BRANCH
expect "Enter passphrase for key '/home/$USERNAME/.ssh/id_rsa': "
send "$pass\r"
expect eof
EOD
Intact answered 9/12, 2015 at 2:6 Comment(1)
This worked lovely for me. My use case is executing a git pull in an enterprise CI setup that is not very flexible, so I need to provide the key passphrase in the CI interface/script.Monolith

© 2022 - 2024 — McMap. All rights reserved.