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 ...
随机推荐
- 重新整理 .net core 实践篇————cookie 安全问题[三十八]
前言 简单整理一下cookie的跨站攻击,这个其实现在不常见,因为很多公司都明确声明不再用cookie存储重要信息,不过对于老站点还是有的. 正文 攻击原理: 这种攻击要达到3个条件: 用户访问了我们 ...
- AcWing 244. 谜一样的牛
有n头奶牛,已知它们的身高为 1~n 且各不相同,但不知道每头奶牛的具体身高. 现在这n头奶牛站成一列,已知第i头牛前面有a头牛比它低,求每头奶牛的身高. #include<bits/stdc+ ...
- Nginx:Nginx配置文件详解
Nginx是一款面向性能设计的HTTP服务器,相较于Apache.lighttpd具有占有内存少,稳定性高等优势. 如下是Nginx的配置详解: ######Nginx配置文件nginx.conf中文 ...
- Java:Java的重写与重载区分
最明显的区别为:重写只存在于子类与父类中,重载存在于一个类中. 具体区别如下: 一.重写(override) override是重写(覆盖)了一个方法,以实现不同的功能.一般是用于子类在继承父类时,重 ...
- buu crackRTF
一.无壳,拖入ida,静态编译一下 整体逻辑还是很清晰,这里我的盲区是那个加密函数,是md5,没看出来,内存地址看错了,之前用黑盒动调一下,发现猜不出来,看某位wp发现有的老哥,直接sha1爆破的,狠 ...
- ftp错误&&详解方案
一.FTP错误代码列表150 文件状态良好,打开数据连接 200 命令成功 202 命令未实现 211 系统状态或系统帮助响应 212 目录状态 213 文件状态 214 帮助信息,信息仅对人类用户有 ...
- C语言:统计字符个数及种类
#include <stdio.h> int main(){ char c; //用户输入的字符 int shu=0;//字符总数 int letters=0, // 字母数目 space ...
- WIN10家庭版 访问WINXP 共享打印机
WIN10家庭版 1.安装对应的打印机驱动 2.打开WIN10计算机---在地址栏中输入:\\计算机XP名称,显示对应的共享资源,直接选择即可.如果无法访问则进行如下第三步 3.设置过程 开始 -设置 ...
- python 抓取异常
aa={"a":2,"b":1} for i in range(10): aa["a"]=aa["a"]-i print ...
- 高校表白App-团队冲刺第二天
今天要做什么 今天要把昨天的activity进行完善,并且加上计时跳转的功能,将其设置为主页面,设置两种跳转功能. 遇到的问题 今天没遇到什么大的问题,只是在进行编写的时候,又出现了R文件无法找到的情 ...