tags:

views:

542

answers:

2

Hi,

I am half-way through writing an Expect script on a Linux server which is supposed to telnet to a router in order to collect some system information. So far my script can successfully make the connection, run a router command, disconnect and terminate.

The command displays a few lines which I need to parse, something I am not sure how to do in Expect. How can I save the output, grep a line, then a column from the line, and finally save the result in a file? If possible, I would like to use Expect entirely rather than a work-around (for example Expect embdded in Bash).

Thanks for your time. jk04

+1  A: 

basically, $expect_out(buffer) [1]. holds the output from last expect match to the current one. you can find your command output there.

and for the string manipulation, you can simply employ the tcl's built-in [2][3].

  1. "How to access the result of a remote command in Expect" http://wiki.tcl.tk/2958
  2. "regexp" http://wiki.tcl.tk/986
  3. "string match" http://wiki.tcl.tk/4385
Dyno Fu
I think $expect_out(buffer) contains only the text matched by your pattern, not all text from last expect match to the current one.
Colin Macleod
i think no, you can check the discussion on "expect_out(buffer) has the content of the previous send", http://wiki.tcl.tk/17378
Dyno Fu
should i say yes? since the text matched by current pattern is the output from last expect to the current one:)
Dyno Fu
A: 

Two tips for expect development:

  • autoexpect to lay out a framework for your automation
  • exp_internal 1 to show verbosely what expect is doing internally. This one is indispensable when you can't figure out why your regular expression isn't capturing what you expect.
glenn jackman