Notes about "Exploring Expect"
Chapter 3
Section "The expect Command": expect_out(0,string) can NOT be written as "expect_out(0, string)", blank before "string" will make a mistake;
Concurrent matching:
expect {
item1 {send cmd1}
item2 {send cmd2}
}
- In spawn process, use "\r" as the return key in send command;
Chapter 5
- Section "Using Parentheses For Feedback" give a clear and concise explanations of how to get feedback in expect.
#!/usr/bin/expect
set timeout 60
expect -re "a(.*)c"
send "expect_out(buffer) = ${expect_out(buffer)}\n"
send "expect_out(0,string) = ${expect_out(0,string)}\n"
send "expect_out(1,string) = ${expect_out(1,string)}\n"
expect -re "e"
send "expect_out(buffer) = ${expect_out(buffer)}\n"
send "expect_out(0,string) = ${expect_out(0,string)}\n"
send "expect_out(1,string) = ${expect_out(1,string)}\n"
Run this script, input "junk abcbcdef" and return, will produces:
$ ./feedback.exp
junk abcbcdef
expect_out(buffer) = junk abcbc
expect_out(0,string) = abcbc
expect_out(1,string) = bcb
expect_out(buffer) = de
expect_out(0,string) = e
expect_out(1,string) = bcb
You can see value of "expect_out(...)" will be refreshed when a expect matches (expect "e"). "buffer" are all things from last buffer ("...cbc") to this match ("e"), which is "de" in this case. "0,string" means all matching strings (note "0, string" will raise a exception for the redundant blank). "1,string" means the first subgroup of "0,string". If there is no subgroup (expressed as parentheses) in current match, (1,string) remains last value ("bcb" in this case). Even "f" at the end of input has been sent to expect, it will not be added to "buffer", because it's not a part of the current match.
Chapter 6
- exp_continue is useful in many circumstances. See its code example in section "Matching Multiple Times" in chapter 6 and section "Prompting For A Password On Behalf Of A Program" in chapter 8;
Chapter 7 & 8
- some frequently used command:
log_file: write output of spawned process to a file;
log_user: send output of spawned process to/not to user;
send_user: expect script send messages to user;
expect_user: continue communicating with the user even after a process has been spawned;
send: expect script send messages to spawned process;
send_log: only write to log (without writing to stdout);
send_error: expect script send messages to stderr;
exp_internal: enable/disable internal diagnostics to stdout or log file (see section "Logging Internal Diagnostics");
proc sendexpect in section "The send_error Command" is useful;
right-wrong-timeout switch in section "The expect_user Command":
expect {
right {
} wrong {
send_error error_message
exit 1
} timeout {
send_error "time out!"
exit 1
}
}
- A program switch between character and line mode. Notice the usage of "stty raw":
send_user "Now we are in line mode. You can use backspace to re-input.\n"
send_user "Continue? y or n\n"
expect_user -re "y|n"
send_user "you press $expect_out(0,string)\n"
stty raw
send_user "Now we are in character mode. You have no chance to re-input.\n"
send "Continue? Enter y or n: \r\n"
expect -re "y|n"
send_user "\nyou press $expect_out(0,string)"
stty should be executed during times when the user is not typing, such as before a prompt rather than after, otherwise there is possibility of losing characters while switching modes. See the end of section "Line Versus Character-Oriented And Other Terminal Modes";
In the modified version of su2 in section "Echoing", I modified "# " to " #" in the last 3rd line, because on my platform (Mint Xfce 14) there are no space after "#" in the prompt of root user;
the get password procedure in section "Echo" is a must-have tool;
If you want exact Bourne-shell semantics, the simplest way is to call system. See section "The system Command";
Chapter 9
arguments experiment after #!
The echo.exp:
version 1:
#!/usr/bin/expect
set argc [llength $argv]
for {set i 0} {$i<$argc} {incr i} {
puts "arg $i: [lindex $argv $i]"
}
version 2:
#!/usr/bin/expect --
...
version 3:
#!/usr/bin/expect -f
...
Now run command ./echo.exp -c "puts hello" 1 2 3, the output is:
version 1:
arg 0: -c
arg 1: puts hello
arg 2: 1
arg 3: 2
arg 4: 3
version 2:
arg 0: -c
arg 1: puts hello
arg 2: 1
arg 3: 2
arg 4: 3
version 3:
hello
arg 0: 1
arg 1: 2
arg 2: 3
So it is clear that only when "-f" is specified, "-c" means "take my arguments as a script". Otherwise "-c" will be treated as a common arguments of the script. "--" explicitly stop any arguments interpreting.
Chapter 17
- this chapter describes how to make expect script a server;
Notes about "Exploring Expect"的更多相关文章
- Expect使用小记
By francis_hao May 31,2017 本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善. Expect是一个可以和交互式程序对话的程序 概述 ...
- /usr/bin/expect介绍
/usr/bin/expect介绍 http://blog.csdn.net/zhu_tianwei/article/details/44180637 概述 我们通过Shell可以实现简单的控制流功能 ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- 交互式shell编程
FQ #!/usr/bin/env shxfce4-terminal -x sudo python ./local/proxy.py 连续执行 gnome-terminal -x bash -c &q ...
- java 文件读写--转载
读文件 http://www.baeldung.com/java-read-file Java – Read from File 1. Overview In this tutorial we’ll ...
- mkpasswd - 为用户产生新口令
总览 SYNOPSIS mkpasswd [ args ] [ user ] 介绍 INTRODUCTION mkpasswd 为用户产生口令并自动应用.它是基于O'Reilly的书<Explo ...
- Exploring Python Code Objects
Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...
- Go 1 Release Notes
Go 1 Release Notes Introduction to Go 1 Changes to the language Append Close Composite literals Goro ...
- Android Weekly Notes Issue #227
Android Weekly Issue #227 October 16th, 2016 Android Weekly Issue #227. 本期内容包括: Google的Mobile Vision ...
随机推荐
- CRM的职能和主要构成模块探索
CRM客户管理系统是随着互联网的快速发展和信息技术的进步而发展起来的,是企业管理客户关系.优化业务流程的首选.客户关系管理可以存储企业获得的客户信息,方便业务人员随时查看和了解客户需求:客户关系管理可 ...
- [Django REST framework - RBAC-基于角色的访问控制、base64编码 、xadmin的使用]
[Django REST framework - RBAC-基于角色的访问控制.base64编码 .xadmin的使用] RBAC-基于角色的访问控制 RBAC 是基于角色的访问控制(Role-Bas ...
- 面试题二:JVM
JVM垃圾回收的时候如何确定垃圾? 有2种方式: 引用计数 每个对象都有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1,计数为0时可以回收: 缺点:无法解决对象循环引用的问题: 可达性分 ...
- php结合redis实现高并发下的抢购、秒杀功能 (转)
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易 ...
- Caffeine缓存的简单介绍
1.简介 在本文中,我们将了解Caffeine,一个用于Java的高性能缓存库. 缓存和Map之间的一个根本区别是缓存会清理存储的项目. 一个清理策略会决定在某个给定时间哪些对象应该被删除,这个策略直 ...
- Vue+axios的四种异步请求,参数的携带以及接收
Vue中axios发送GET, POST, DELETE, PUT四种异步请求,参数携带和接收问题 web.xml配置如下 1.GET请求 发送GET请求: <!--params是关键字,说明所 ...
- JDK 和 CGLib 实现动态代理和区别
JDK 和 CGLib 实现动态代理和区别 在日常的开发中,Spring AOP 是一个非常常用的功能.谈到 AOP,自然离不开动态代理. 那么,基于 JDK 和 CGLib 如何实现动态代理,他们之 ...
- 浅析java中的IO流
在java中IO类很庞大,初学的时候觉得傻傻分不清楚.其实java流归根结底的原理是普通字节流,字节缓冲流,转换流.最基础的是普通字节流,即从硬盘读取字节写入到内存中,但在实际使用中又发现一些特殊的需 ...
- Python自动化测试面试题-MySQL篇
目录 Python自动化测试面试题-经验篇 Python自动化测试面试题-用例设计篇 Python自动化测试面试题-Linux篇 Python自动化测试面试题-MySQL篇 Python自动化测试面试 ...
- 第七篇 -- photoshop cs6 激活
下载photoshop cs6破解版 下载amtlib.dll 破解就是将amtlib.dll替换,路径:C:\Program Files\Adobe\Adobe Photoshop CS6 (64 ...