自动化交互式脚本

一、安装except

1.1 安装依赖tcl

1
2
3
4
5
wget https://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz
tar zxvf tcl8.4.19-src.tar.gz
cd tcl8.4.19/unix && ./configure
make
sudo make install

1.2 下载并安装except

1
2
3
4
5
6
7
wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz
# tar zxvf expect5.45.tar.gz
# cd expect5.45
# ./configure --with-tcl=/usr/local/lib --with-tclinclude=../tcl8.4.19/generic
# make
# sudo make install
# ln -s /usr/local/bin/expect /usr/bin/expect

二、用法

2.1 概述

1
except [选项][ -c cmds] [ [ -[f|b] ] cmdfile] [ args]

2.2 常见用法

  1. 开启子程序
1
spawn program
  1. 自动交互
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
expect {

"字符串A" { send "字符串B"; exp_continue }

"字符串C" { send "字符串D"; exp_continue }

...

}

expect eof

注意事项

  1. #!/usr/bin/expect作为开头

  2. expect文件以.exp作为后缀

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy