一 什么是expect
1 Expect is a tool for automating interactive applications such as telnet, ftp, 2 passwd, fsck, rlogin, tip, etc. Expect really makes this stuff trivial. Expect 3 is also useful for testing these same applications. And by adding Tk, you 4 can also wrap interactive applications in X11 GUIs. 5 6 Expect can make easy all sorts of tasks that are prohibitively difficult with 7 anything else. You will find that Expect is an absolutely invaluable tool - 8 using it, you will be able to automate tasks that you've never even 9 thought of before - and you'll be able to do this automation quickly and 10 easily.
expect 是一个用于构建交互式自动化(telnet,ftp等)的工具,可以配合tk构建x11gui.expect能把你日常交互的工作变得自动,简单.(CLI下测试网络设备).
二 安装
expect 基于tcl (怀念以前的tcl,ATF啊,时间过的好快,眨眼快1年了)
unix系安装 yum -y install tcl expect 敲入 expect
windows安装 http://www.activestate.com/activetcl/downloads 下载自己的版本
切换到你的目录tcl/bin/ ,python 包管理是easy_install 和pip ,tcl 的包管理工具是teacup
出现这个问题
1 C:\windows\system32>teacup.exe install Expect 2 Resolving Expect ... Not found in the archives. 3 4 While a more fuzzy search disregarding letter case and accepting 5 substrings was done, we are sorry to say that it yielded no possible 6 candidates for installation either. 7 8 Questions to consider: 9 Have you spelled the name correctly ? 10 Including the proper case of characters ? 11 12 Note that teacup's 'search' command allows you to locate packages by 13 subject, categories, and the like. 14 15 16 Aborting installation, was not able to locate the requested 17 entity.
发现找不到,官网看了下,没有x64版本,x86还是5.43版本. 另外tcl85为了控制包大小,去掉了expect. 84内置了expect,脚本头部package require Expect.想在win上用还是装84吧.
三 实例
1. 写一个ping 的tcl 脚本
运行结果
2. 写一个expect
运行结果
*** 脚本运行前肯定要给执行权限嘛
expect 例子写的不好,大家可以写个模拟smtp163 之类的练手
语法参考:
http://www.tcl.tk/man/tcl8.5/tutorial/
http://expect.sourceforge.net/
本站博文皆为原创,转载请标明出处,小三爷在此谢过了~~!