Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Expect-out-buffer.html

Our email: info@wellho.net • Phone: 01144 1225 708225

Expect_out(buffer)

Posted by sharmila (sharmila), 8 December 2005
I have a expect script for a device.
After sending a command,I am trying to print out the value in expect_out(buffer).Instead of printing only the result of the command,expect_out(buffer) has the command and result of the command.I just need only the result of the command.And then from the result which has many lines ,I just need the last line of the result

Posted by admin (Graham Ellis), 9 December 2005
The immediate answer is to use split and lindex to extract the information you need - something like:

set iwant [lindex [split $expect_out(buffer) "\n"] end]

but you may need to modify that if you've got different line terminators or your incoming string ends with a terminator.  You could also consider using a regular expression.

More generally, I'm not a great fan of running an expect command that reads in a huge response and then deals with it; too much scope for problems that are hard to identify if it goes wrong, and I would personally write a loop which reads back the results line by line; in that case you can simple handle the last line when you get it.   You'll see where I've used many examples of this technique in our training examples on expect - the links to individual source code examples are on our module listing pages here, here and here.

Posted by sharmila (sharmila), 9 December 2005
Thanks Graham..

Still I couldnt extract the last line alone.
I use the same line u tlod
set iwant [lindex [split $expect_out(buffer) "\n"] end]
puts $iwant
Even after the split command, the variable iwant prints what is in the expect_out(buffer) that is iwant has the same contents as expect_out(buffer).
How to solve this....
Thanks for ur help...
Sharmila.












Posted by admin (Graham Ellis), 9 December 2005
Did you try with the different (appropriate for your OS) line terminators as suggested too?   Did that make any difference?  What is your OS?   Have you tried saving the result of the split?  Try running an llength on that result to help you analyse what's happening.

My suggestion of following a loop was an stong one - could you do that as it would give you far more debuggable, robust and maintainable code.

Posted by mansoorx (mansoorx), 22 February 2006
Hello,
          I am Mansoor Nawaz, Opentalk's new member "mansoorx", i would like to ask question about Expect,
i am writing a script which i use to display interface descriptions,


I want to store last command result in the new file, how can i do it, because the result of last command is very long e.g,

ConRoom1>sh int des
Interface                      Status         Protocol Description
Vl1                             up             up
Vl2                             admin down     down
Vl103                         admin down     down
Fa0/1                          up             up
Fa0/2                          up             up
Fa0/3                          down           down
Fa0/4                          down           down
Fa0/5                          down           down     Trunk
Fa0/6                          down           down
Fa0/7                          down           down
Fa0/8                          down           down
Fa0/9                          down           down
Fa0/10                         down           down
Fa0/11                         down           down     Testing
Fa0/12                         down           down
Fa0/13                         down           down
Fa0/14                         down           down
Fa0/15                         down           down
Fa0/16                         down           down
Fa0/17                         down           down
Fa0/18                         down           down
Fa0/19                         down           down
Fa0/20                         down           down
Fa0/21                         down           down
Fa0/22                         down           down
Fa0/23                         down           down
Fa0/24                         up             up
Gi0/1                          down           down
Gi0/2                          down           down


and i am using cisco 2950 switch.

I am waiting for ur Positive reply & in the last i appriciate ur OpenTalk site which is very much helpfull for a student like me.

Thank You


Posted by technocrat2004 (technocrat2004), 22 February 2006
Dear Sharmila,
I have a sujestion................
put the entire content of expect_out into a file. Read line by line and use regexp.. I have been doing this and was susccess fullll...... If you are more clear on the problem I think I can help you

Posted by technocrat2004 (technocrat2004), 22 February 2006
Dear Mansoor,

Try out the following..... I think Sharmila's solution will help you also

set var $expect_out(buffer);
set fid [open filname w+]
put $fid $var


Regards,
Technocrat

Posted by mansoorx (mansoorx), 22 February 2006
Thank you for this reply i am trying this script,  but not working still, i am very thankfull to u, if usend me complete modified script to me
Thank You.



Posted by technocrat2004 (technocrat2004), 22 February 2006
Hi Bose...............,
Please avoid the " " in puts. It should be

puts $fid $var.
You are telling to put the the contents of $var into the the file

After this also if it does not work please go to the file manually and check whether the file is created......


Thankssssssssss

Posted by mansoorx (mansoorx), 22 February 2006
Now i am writing this Modified script, but the output is still ">"  when i see the contents of the file   "/tmp/abc" ,
Thank You.



Posted by technocrat2004 (technocrat2004), 23 February 2006
hello Bos,.
I did not get a router to check out how ever i tried with two linux PC and it worked fine...

Here is the code...


set TlntIp "<ipadd>"
set timeout 30
spawn ssh $TlntIp
expect -i $spawn_id "password:"
exp_send -i $spawn_id "password\r"
expect {
        -i $spawn_id "root]#" {exp_send -i $spawn_id "ls -l\r"}
       }
expect "]#";
exp_send -i $spawn_id "ls -l\r";
set fid [open /root/ouputIntf.txt w+];
set tempvar1 $expect_out(buffer);
puts "this is tempvar \n $tempvar1";
puts $fid $tempvar1 ;


I ll try with router and ll let you know...
byeeeeeeeeee

Posted by mansoorx (mansoorx), 24 February 2006
Hello,
         I am using this script, but still out put is not displayed or store in the /tmp/abc file,  


Posted by admin (Graham Ellis), 26 February 2006
on 02/24/06 at 04:32:02, mansoorx wrote:
expect ">"
send "sh int des\r"
set fid [open /tmp/abc.txt w+];
set tempvar1 $expect_out(buffer);
puts "this is tempvar \n $tempvar1";
puts $fid $tempvar1 ;


I am desparatley waiting for a proper solution of this script, thankyou.



I think you need to add a further expect command to await the response from your sh int des command - if that finishes with another shell prompt, then what you need would be

Code:
expect ">"
send "sh int des\r"
expect ">"
set fid [open /tmp/abc.txt w+];
set tempvar1 $expect_out(buffer);
puts "this is tempvar \n $tempvar1";
puts $fid $tempvar1 ;


As your code currently stands, you send the "sh int des" command and then without waiting for it to actually run you save the contents of the buffer into a file .... so you get the old contents.

With the extra expact line, you expect script will pause while the router does its stuff and the response is put in the buffer, which should then save to the file.




Posted by mansoorx (mansoorx), 27 February 2006
Thank you for correction ,
I just change my scripts according to ur instruction,

Thank You.




Posted by admin (Graham Ellis), 27 February 2006
Quote:
can't read "tempvar1": no such variable


That means that you are trying to use the contents of the variable tempvar1 before you have set it to anything.

In your code, you set a variable called tempvarl (the last character of that being the letter L) and then use a variable called tempvar1 (the last character being the digit 1).  If you change the names to being identical - either tempvar1 or tempvarl - you should clear the error.

Posted by mansoorx (mansoorx), 27 February 2006
Thank You, Graham

Now it start to work, Actually theses two digits are far from each other, due to which i cant see it.
Thank You Again.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho