Expect是Unix系统中用来进行自动化控制和测试的软件工具,由Don Libes制作,作为Tcl脚本语言的一个扩展,应用在交互式软件中如telnet,ftp,Passwd,fsck,rlogin,tip,ssh等等。该工具利用Unix伪终端包装其子进程,允许任意程序通过终端接入进行自动化控制;也可利用Tk工具,将交互程序包装在X11的图形用户界面中。
以下是一个telnet自动登录的小脚本
#!/usr/bin/expect --
# 向远程服务器请求打开一个telnet会话,并等待服务器询问用户名
spawn telnet 127.0.0.1
expect "username:"
# 输入用户名,并等待服务器询问密码
send "admin\r"
expect "Password:"
# 输入密码,并等待键入需要运行的命令
send "admin\r"
expect "%"
# 输入预先定好的密码,等待运行结果
send "create vlan test\r"
expect "%"
# 将运行结果存入到变量中,显示出来或者写到磁盘中
set results $expect_out(buffer)
# 退出telnet会话,等待服务器的退出提示EOF
send "exit\r"
expect eof
利用expect, 可以方便的完成ftp,telnet, passwd等自动操作. 也可以进行一系列自动化测试! :)
参考:
Expect on SourceForge (current) Official homepage (very outdated!) The Tcler's Wiki -- Expect page Perl Expect.pm module Pexpect a Pure Python Expect-like module Expect Scripting Tutorial Empty - expectlike tool to run command-line interactive programs in UNIX shell-scripts Expect-lite -- a wrapper for expect, making automation easy, quick, and fun Bulletproof: Reliable CLI interface using Expect ExpectJ - a Java implementation of the Unix expect utility
本文介绍了一个用于Unix系统的自动化控制和测试工具——Expect。该工具作为Tcl脚本语言的扩展,能够实现对交互式软件(如telnet、ftp等)的自动化控制。文章还提供了一个telnet自动登录的小脚本示例。
567

被折叠的 条评论
为什么被折叠?



