zoukankan      html  css  js  c++  java
  • Automating Capistrano Password Prompts with Expect

    Automating Capistrano Password Prompts with Expect

    Automating Capistrano Password Prompts with Expect

    I just started using Capistrano for deploying my Rails applications (like Snapclean.me"). I also just started using capistrano_rsync_with_remote_cache 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 Expect can do this easily, my only problem is I don’t remember Tcl 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 expect
    stty -echo
    send_user -- "Login Password: "
    expect_user -re "(.*)\n"
    send_user "\n"
    stty echo
    set the_password $expect_out(1,string)
    
    spawn cap deploy
    expect {
      -re " *\[Pp\]assword: *" {
        send "$the_password\n"
        exp_continue
      }
    }
    

    I put the above script in a file called wrap-cap, and did a chmod 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.

    Kyle Burton, 6 Jul 2010 – Philadelphia PA

  • 相关阅读:
    mysql锁 实战测试代码
    Memcache教程 Memcache零基础教程
    Moneybookers API支付方式开发 步骤
    dede判断当前文章
    Windows下的Memcache安装 linux下的Memcache安装
    Ecshop:后台添加新功能栏目以及管理权限设置
    解决echsop兼容jquery(transport.js的冲突)的问题
    PHP AJAX JSONP实现跨域请求使用实例
    shell基础 -- 基本语法
    神器之strace
  • 原文地址:https://www.cnblogs.com/lexus/p/2363309.html
Copyright © 2011-2022 走看看