Expect, an extension to the Tcl (Tool Command Language) scripting language, and it is a scripting language to interface with programs such as FTP, telnet, fsck, ssh, and others that normally cannot be automated from a shell script.

使用情景

Expect 可以实现自动交互的功能,所以,很常用的一种情景就是为我们自动输入密码。如:ssh 登录,git push/pull 等情景。

安装

  • Ubuntu

sudo apt-get install expect
  • CentOS

sudo yum install expect

Expect 需要 Tcl 编程语言的支持,所以必须先安装 Tcl。

实例

Expect 脚本使用的是 Tcl 的语法

  • 批量更新 git 仓库的内容(git pull)

#!/usr/bin/expect set password "123456" foreach app { /root/app/ic/ic-server/ /root/app/ic/ic-client/ } { cd $app spawn git pull expect { "Username*" {send "lizs\r"} "*assword*" { send $password interact } } }

更多使用参考 Expect-wiki.