Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you save and parse a command output in Expect?

Tags:

expect

tcl

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

like image 678
jk04 Avatar asked Dec 04 '22 13:12

jk04


2 Answers

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.
like image 187
glenn jackman Avatar answered Dec 27 '22 13:12

glenn jackman


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
like image 40
Dyno Fu Avatar answered Dec 27 '22 12:12

Dyno Fu