Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

capturing output of expect send from a tcl script

2,665 views
Skip to first unread message

Ravi Parimi

unread,
Apr 1, 2004, 1:30:02 PM4/1/04
to
Hi,
I have a tcl script which sends out commands to a remove server
(by first logging in through ssh). I wish to capture the output of the
command into a variable. I am trying to make a few changes to tcl scripts
written by someone else..

I've tried:

set spawn_id [ $dut get_sshspid ]
send "show server statistics\r"
expect "*"
puts "This is the output of the command: "
puts $expect_out(buffer)

The above snippet doesnt work and reports the error
Failed to execute due to error : can't read "expect_out(buffer)": no such
variable

I am fairly new to expect/tcl and am not sure if $expect_out(buffer) would
be defined in a tcl script..Can someone pleae show me how I can save
the output of a command into a variable?

Thanks,
--ravi

Bruce Hartweg

unread,
Apr 1, 2004, 5:37:36 PM4/1/04
to

Ravi Parimi wrote:

expect * doesn;t do what you think it does, it says zero
or more chars - so you get zero - it matches so it doesn't
even look for more. since you never look for anything, anywhere
expect_out isn;t evr being filled in.

you want to look for specific things

generally you want

expect $prompt ;# wait for a prompt
send $command\r ;# send command
expect $command\n ;# see command echo back
expect -re "(.*)$prompt ;# see next prompt

now expect_out(buffer) should be everything (including prompt)
and excpect_out(string,1) should be results of your command

if you command returns large amounts of data you might have
to expand that by grabbing chunks at a time until you see
the prompt.

Bruce

Cameron Laird

unread,
Apr 1, 2004, 6:19:02 PM4/1/04
to
In article <LP0bc.10$m8...@dfw-service2.ext.ray.com>,
Bruce Hartweg <bruce...@hartweg.us> wrote:
.
.

.
>you want to look for specific things
>
>generally you want
>
>expect $prompt ;# wait for a prompt
>send $command\r ;# send command
>expect $command\n ;# see command echo back
>expect -re "(.*)$prompt ;# see next prompt
>
>now expect_out(buffer) should be everything (including prompt)
>and excpect_out(string,1) should be results of your command
>
>if you command returns large amounts of data you might have
>to expand that by grabbing chunks at a time until you see
>the prompt.
.
.
.
All true. If one insists, it's also possible to convert Expect into a line-
oriented engine, by expect-ing for newline <URL: http://wiki.tcl.tk/3178 >.
--

Cameron Laird <cla...@phaseit.net>
Business: http://www.Phaseit.net

Ravi Parimi

unread,
Apr 2, 2004, 8:51:33 PM4/2/04
to
Bruce,


Thank you very much for your reply. I tried the exact sequence of commands
you mentioned and was able to look at the server output in
expect_out(buffer). Thanks again!

--ravi

0 new messages