expect Q&A

1. Can Expect automatically generate a script from watching a session?

From: libes (Don Libes)
To: pete@willow24.cray.com
Subject: Expect
Date: Fri, 12 Oct 90 17:16:47 EDT
>I like "Expect" and am thinking of using it to help automate the
>testing of interactive programs.  It would be useful if Expect had a
>"watch me" mode, where it "looks over the shoulder" of the user and
>records his keystrokes for later use in an Expect script.
>
>(Red Ryder and other Macintosh telecommunications packages offer this
>sort of thing.  You log onto Compuserve once in "watch me" mode, and
>RR keeps track of the keystrokes/prompts.  When you're done you have a
>script that can be used to log onto Compuserve automatically.)
>
>Before I look into adding a "watch me" feature, I thought I should
>ask: has this been done already?
>
>I'll say again that I like the tool a lot--nice work!  There are other
>people here using it for things like the testing of ksh, which
>responds differently to signals when not used interactively.
>
>-- Pete

The autoexpect script in Expect's example directory does what you want.

Don


2.How do I send control characters or function keys?

Sending control characters is easy: just write a send command and enter the control character you want to send.

I like to enter most control characters literally - since the resulting script is more readable. However, your editor has to allow this. For example, suppose you want to send a Control-A. Conventionally, most editors display this as ^A, but you can't just enter ^ and A. (This will just send those two characters.) Most editors have some simple quoting mechanism that lets you enter the next character literally. For example, using emacs, I can enter ^Q^A and that will add the single character ^A.

Alternatively, Tcl provides a way of encoding using octal or hex. The octal encoding mechanism is less error-prone than hex so I'll demonstrate using octal. For example, since ^A has the octal value 1 in ASCII, to send a ^A:

    send "\01"

To send function keys, you need to know one more thing - what characters are assigned to a function key. Function keys are generally specific to the type of terminal (or terminal emulator) you are using, so it's easiest to just try pressing the function key and see what you get. In order to get the clearest picture, I use the od program to help me. Here are the steps I use:

  1. start od with the -c option
  2. press the function key
  3. press the return key
  4. press ^D

Here's what it looks like when I do that to find out what my F1 key generates:

    % od -c
^[[11~
0000000 033   [   1   1   ~  \n
0000006
%

The string between the 0000000 and \n is what we want. You can translate that into a send command like this one:

    send "\033\[11~"

Notice that the 033 was an octal specification already so I put a backslash in front of it to tell Tcl to treat it that way. I also put a backslash in front of the [ to prevent Tcl from trying to execute the next thing as a command.

One complication: characters may massaged (stolen or replaced with other characters) by the terminal driver. For example, in the example above, all the characters produced by the function key were sent to od, but the return character I pressed afterward was translated to a newline - which showed up as in the output of od). Unfortunately, there is no trivial way of turning off all such processing, but you can get rid of the usual offenders by executing the command:

    stty -isig -icanon -exten -ixon -ixoff

This will take care of most driver translations - except for return to newline which you probably don't want to mess with. Since ^D will stop working, you'll need to enter enough characters to get od to finish a line. Enter at least 15 more characters and press return to see some output from od.

Most systems support "stty raw" as a shortcut for the stty command above however not all do. In fact, some systems may not support all the options above. You may have to do some experimentation. Also note that some shells automatically undo any stty command that they "don't like". For example, the Z-shell (zsh) requires "ttyctl -u" before doing a stty like the ones above.

Hopefully, this should answer most of the questions. If you have further questions, please check the book. It has lots of examples of common literal, octal and hex sequences, and using these with send. Plus, it also talks about more sophisticated issues such as using function keys with termcap/terminfo.

Don


摘自:
http://expect.nist.gov/FAQ.html#q23


posted on 2010-04-13 16:42 挑灯看剑 阅读(458) 评论(0)  编辑 收藏 引用 所属分类: 工作随笔

只有注册用户登录后才能发表评论。
<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

公告

【自我介绍】 08年南开大学硕士毕业 最近关注:算法、Linux、c++、高并发 爱好:滑旱冰、打乒乓球、台球、保龄球

常用链接

随笔分类(139)

文章分类

我常去的网站

技术博客(都是大牛)

技术站点

搜索

积分与排名