fasheng的博客

自在 平和 长久 共一

利用 expect 简化 goagent 配置过程

| Comments | posted in 代码_系统技巧 with tag expect, goagent

配置 goagent 过程中,若一次设置多个 APPID,则需要重复输入多次 gmail 和密码,较为繁琐,于是写了一个 expect 脚本来简化该过程,代码如下[1]

#! /usr/bin/expect -f

set timeout 60

if {$argc != 2} {
    set scriptname [lindex [split $argv0 "/"] end]
    send_user "Usage: $scriptname \"APPID01|APPID02..\" <email>\n"
    exit
}

proc getpass pwprompt {
    set oldmode [stty -echo -raw]
    send_user "\n$pwprompt"
    set timeout -1
    expect_user -re "(.*)\n"
    send_user "\n"
    eval stty $oldmode
    return $expect_out(1,string)
}

set appids [lindex $argv 0]
set appidlist [split [lindex $argv 0] "|"]
set email [lindex $argv 1]
set password [getpass "Please enter password: "]

set python "python2"
set uploader "/usr/share/goagent/server/uploader.zip"
set command "$python $uploader"

spawn $python $uploader
expect -nocase "appid:" {send "$appids\r"}
for {set i 0} {$i < [llength $appidlist]} {incr i} {
    set appid [lindex $appidlist $i]
    expect -nocase "application:" {send_user "\n\n==> Setup up appid: $appid"}
    expect -nocase "email:" {send "$email\r"}
    expect -nocase "password for" {send "$password\r"}
    expect -nocase "email:" {send "$email\r"}
    expect -nocase "password for" {send "$password\r"}
}

expect "上传成功" {send "\r"}
interact

使用方法很简单,运行脚本,而后输入密码即可:

sudo ./expect_goagent.tcl "appid01|appid02" your@gmail.com

Comments