开发者

How to use expect and git clone?

I'm trying to clone from my private git repository using expect to enter the password in one line.

I'm trying something like:

expect -c 'spawn git clone user@..*.*:/var/.../repository.git/; e开发者_如何学运维xpect "(yes/no)?"; send "yes\n"; expect "password:"; send "my_password\n";interact'

But, it doesn't work. What am I doing wrong? How can I fix the command line above?

Thanks, Arshavdki Alexander


Why don't you just use something more secure than a password and simplify your life?

An ssh key with no password will be significantly more secure and not prompt at all.

If it must be 100% self-contained, then consider making the script output the SSH key, use it once, then remove it. (That should leave your security situation unchanged: the password is effectively in the script, which is acceptable for you.)

# Output SSH key for no-password login. (Could use mktemp instead)
cat > /tmp/ssh_key.$$ <<EOT
-----BEGIN RSA PRIVATE KEY-----
blahblahblah
-----END RSA PRIVATE KEY-----
EOT

# Make a SSH wrapper to do the right thing
cat > /tmp/git_ssh.$$ <<EOT
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /tmp/ssh_key.$$ "$@"
EOT
chmod +x /tmp/git_ssh.$$
export GIT_SSH=/tmp/git_ssh.$$

# Done!
git clone user@host:path/to/repo

# Cleanup
rm -f /tmp/git_ssh.$$ /tmp/ssh_key.$$

Yes the script looks unwieldy but (modulo bugs) it is self-contained pretty useful for automation.


You could avoid using expect by directly entering password in the url

git clone https://username:password@github.com/username/repository.git

Please note - this causes your password to be stored in the .git files, making this a permanently insecure option.

However, you can leave out the password:

git clone https://username@github.com/username/repository.git

git will ask you for password and it will not be saved in .git/config nor in your bash history

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜