alexarsh alexarsh - 1 year ago 89
Git Question

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/; expect "(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

Answer Source

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

# 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.$$ "$@"
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.