expect脚本

Expect 脚本
Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。 Expect的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:Expect是一个用来实现自动交互功能的软件套件(Expect [is a] software suite for automating interactive tools)。使用它系统管理员的可以创建脚本用来实现对命令或程序提供输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些 输入都需要手工输入进行的。Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。甚至可以实现实现简单的BBS聊天机 器人。 :)
Expect是不断发展的,随着时间的流逝,其功能越来越强大,已经成为系统管理员的的一个强大助手。Expect需要Tcl编程语言的支持,要在系统上运行Expect必须首先安装Tcl。
Expect工作原理 :
从最简单的层次来说,Expect的工作方式象一个通用化的Chat脚本工具。Chat脚本最早用于UUCP网络内,以用来实现计算机之间需要建立连接时进行特定的登录会话的自动化。
Chat脚本由一系列expect-send对组成:expect等待输出中输出特定的字符, 通常是一个提示符,然后发送特定的响应。例如下面的Chat脚本实现等待标准输出出现Login:字符串,然后发送somebody作为用户名;然后等待 Password:提示符,并发出响应sillyme。

The Expect Home Page1. 下载了expect.tar.gz;
2. 解压;
#tar zxvf expect.tar.gz
3. 安装;
#./configure
./configure
configuring Expect 5.43.0
checking for autoconf... yes
checking configure up to date... yes
checking host system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized

checking target system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized

checking build system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized

checking shell to use within Make...
checking for gcc... gcc
checking whether the C compiler (gcc  ) works... yes
checking whether the C compiler (gcc  ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking for building with threads... no (default)
checking for Tcl configuration... configure: warning: Can't find Tcl configuration definitions
./configure: line 1106: #: No such file or directory

看来要找Tcl;
#whereis tcl
tcl: /usr/lib/tcl8.4 /usr/share/tcl8.4
改变tcl目录,重试;
#./configure --with-tcl=/usr/lib/
configuring Expect 5.43.0
checking for autoconf... yes
checking configure up to date... yes
checking host system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized

checking target system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized

checking build system type... Invalid configuration `x86_64-unknown-linux-gnu': machine `x86_64-unknown' not recognized

checking shell to use within Make...
checking for gcc... gcc
checking whether the C compiler (gcc  ) works... yes
checking whether the C compiler (gcc  ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking for building with threads... no (default)
checking for Tcl configuration... configure: error: /usr/lib/ directory doesn't contain tclConfig.sh
看来要找tclConfig.sh;
# find / -name "tclConfig.sh"
什么都没有找到!看来要重新安装tcl;
进入The Expect Home Page
跳转到tcl主页the Tcl Developer Xchange web site
选择Tcl8.5, 进入http://www.tcl.tk/software/tcltk/8.5.html
点击Download Tcl/Tk 8.5.8 Source Releases
选择下载tcl8.4.19-src.tar.gz,和tk8.4.19-src.tar.gz

a. 编译安装tcl8.4.19;
#tar zxvf tcl8.4.19-src.tar.gz
#cd tcl8.4.19
#cd unix
#configure
最后几句:
... ...
updating cache ./config.cache
creating ./config.status
creating Makefile
creating dltest/Makefile
creating tclConfig.sh
#make
#make install
b. 编译安装tk8.4.19;
同样的步骤,不再详述;

回到断点,继续安装expect;
# ./configure
checking for sin... no
checking for Tcl private headers... checking for tclInt.h... no
configure: error: Can't find Tcl private headers

# find / -name "tclInt.h"
/home/guoq/osrc/tcl8.4.19/generic/tclInt.h

# ./configure --help
... ...
--with-tclinclude       directory where tcl private headers are
... ...

#./configure --with-tclinclude=/home/guoq/osrc/tcl8.4.19/generic/
... ...
checking for Tcl private headers... (cached) found in /home/guoq/osrc/tcl8.4.19/generic
updating cache .././config.cache
creating ./config.status
creating Makefile

看来ok了!
#make
#make install

4. 写个脚本试试;
#!/usr/local/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30

spawn ssh root@$ipaddress
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
interact

哇哈哈!I am in.