我正在尝试使用 Expect 在一行中输入密码来从我的私人 git 存储库进行克隆。
我正在尝试类似的事情:
期待 -c 'spawn git clone user@ 。.* . *:/var/.../repository.git/; 期待“(是/否)?”;发送“是\n”;期待“密码:”;发送“my_password\n”;交互'
但是,这不起作用。我究竟做错了什么?如何修复上面的命令行?
谢谢,阿尔沙夫基·亚历山大
为什么不使用比密码更安全的东西来简化您的生活呢?
没有密码的 ssh 密钥将更加安全,而且根本不会提示。
如果它必须是 100% 独立的,那么请考虑让脚本输出 SSH 密钥,使用一次,然后将其删除。(这应该使您的安全状况保持不变:密码在脚本中有效,这对您来说是可以接受的。)
# 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.$$
Run Code Online (Sandbox Code Playgroud)
是的,该脚本看起来很笨拙,但(模数错误)它是独立的,对于自动化非常有用。