Automating Capistrano Password Prompts with Expect
I just started using for deploying my applications (like "). I also just started using to help push releases out faster than the
:copy
deploy strategy.I’m very happy with how much faster it is than the a
:copy
, but I’m impatient and having to provide the password more than once per invocation is frustrating to me. I know the old Unix standby can do this easily, my only problem is I don’t remember very well and every time I’ve done this I forget how / what I did. This post is a write up so I know where to come back to the next time I need to do this (and I know I’ll run into it again).Spawn and Expect
Conceptually the basic use of Expect is very straight forward. You execute another program and you can register handlers that will be invoked when the program emits specific output. In the case of executing capistrano, and it executing other commands, I’m looking for anything that looks like a password prompt. When a password prompt is emitted I want to send the password.
The script below does exactly that, first asking the user for the password (disabling terminal echo) before spawning the capistrano command itself.
#!/usr/bin/env expectstty -echosend_user -- "Login Password: "expect_user -re "(.*)\n"send_user "\n"stty echoset the_password $expect_out(1,string)spawn cap deployexpect { -re " *\[Pp\]assword: *" { send "$the_password\n" exp_continue }}I put the above script in a file called
wrap-cap
, and did achmod 755 ./wrap-cap
on the file.The
exp_continue
is used to reset (or loop) expect so that it will continue to look for password prompts and provide the password as many times as it is requested – since there are various commands that may ask for it (rsync, ssh, sudo, etc.) and some of them are optional (especially sudo), this ‘looping’ behavior is very handy.Expect is a great Unix tool for developers and system administrators to have in their toolboxes.