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

  • 相关阅读:
    [转]敏捷软件开发 需求分析
    Windows服务控制命令SC
    [转]敏捷测试工程师的十条法则
    大道至简:如何准确把握趋势 一
    Sqlserver xml 类型操作
    微软Teched2010大会一日游记
    [转]任务、用例、用户故事和场景
    ASUS TF600T上手体验
    Windows 8的各种小发现——不断更新中
    Surface Pro电磁笔故障
  • 原文地址:https://www.cnblogs.com/lexus/p/2363309.html
Copyright © 2011-2022 走看看