Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112252
  • 博文数量: 22
  • 博客积分: 835
  • 博客等级: 准尉
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 21:23
文章分类

全部博文(22)

文章存档

2011年(1)

2009年(21)

我的朋友

分类: LINUX

2009-12-11 15:42:58

Expect使用Tcl作为语言核心,它的最主要用途就是以非交互性的方式实现了所有交互性的功能。
很多工具在使用的时候可能都提供了选项来禁止交互式的提问,或假定一个默认回答。这种实现方法往往太粗糙了,
因为每个提问的情景可能都不一样。expect则可以作到根据提问的具体内容来选择进行不同的操作。
Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language,
for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others.
It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal.
With Tk, interactive applications can be wrapped in X11 GUIs.
expect虽然是基于TCL,TCL实际上是一个子程序库,这些子程序库可以嵌入到程序里从而提供语言服务。但它还提供了一些Tcl所没有的命令:
 Spawn命令:激活一个Unix程序来进行交互式的运行
 send命令:向进程发送字符串
 expect命令:等待进程的某些字符串,expect支持正规表达式并能同时等待多个字符串,并对每一个字符串执行不同的操作。
    expect还能理解一些特殊情况,如超时和遇到文件尾
    
如下是刚写的一小段脚本用来修改用户密码:
#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
puts "user:$username, password:$password.\n"
spawn passwd $username
expect "*password: "
send "$password\n"
expect "*password: "
send "$password\n"
expect eof
执行如下:expect tempuser temppass
user:tempuser, password:temppass.
spawn passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

最好的参考资料是:man expect
官方网站:
阅读(1685) | 评论(0) | 转发(0) |
0

上一篇:sed&awk学习总结3

下一篇:Linux入门学习建议

给主人留下些什么吧!~~