1

I've searched for this & I haven't found an answer that really works for me.

I want to set the output of a command sent via expect as a variable, which I will parse with TcL. This will mostly be used on appliances that don't have TcL installed. Things like firewalls, routers & switches, etc.

something like this:

send "show interface status"

#output of show command on device  
Port      Name               Status       Vlan       Duplex  Speed Type
Gi1/1     trunk to switch    notconnect   100          auto   auto 10/100/1000-TX
Gi1/2     this is a test por notconnect   100          auto   auto 10/100/1000-TX
Gi1/3                        notconnect   routed       auto   auto 10/100/1000-TX
Gi1/4                        notconnect   400          auto   auto 10/100/1000-TX

I'd like to have the variable to be a list where normally I'd use this on the device if it had TcL:

set showInterface [split [exec "show interface status"] \n]
Cory
  • 23
  • 1
  • 4

1 Answers1

0

After using the send command, you need to use expect command to get all the show command output something like ,

#This is a common approach for few known prompts
#If your device's prompt is missing here, then you can add the same.
set prompt "#|>|:|\\\$"; # We escaped the `$` symbol with backslash to match literal '$'

#Your code to telnet to the device here...

# This is to clean up the previous expect_out(buffer) content
# So that, we can get the exact output what we need.
expect *; 

send "show interface status\r"; # '\r' used here to type 'return' .i.e new line
expect -re $prompt; # Matching the prompt with regexp

#Now, the content of 'expect_out(buffer)' has what we need
set output $expect_out(buffer);

set interfaces [ split $output \n ]; # Getting each interfaces info in a list.

You can have a look at here to know more about the why you need expect *.

Update :

By default, the limit of expect's buffer size is enough to guarantee that patterns can match up to the last 2000 bytes of output. This is just the number of characters that can fit on a 25 row 80 column screen. (i.e.25*80=2000)

The maximum size of matches that expect guarantees it can make is controlled with the match_max command. As an example, the following command ensures that expect can match program output of up to 10000 characters.

match_max 10000 

The figure given to match_max is not the maximum number of characters that can match. Rather, it is a minimum of the maximum numbers of characters that can be matched. Or put another way, it is possible to match more than the current value but larger matches are not guaranteed.With no arguments, match_max returns the value for the currently spawned process.

%
% package require Expect
5.43.2
% match_max
2000
% match_max 10000
% match_max
10000
%

Setting the buffer size sufficiently large can slow down your script, but only if you let the input go unmatched. As characters arrive, the pattern matcher has to retry the patterns over successively longer and longer amounts of input. So it is a good idea to keep the buffer size no larger than you really need.

Community
  • 1
  • 1
Dinesh
  • 16,014
  • 23
  • 80
  • 122
  • This works great, but it seems that the $expect_out(buffer) isn't large enough to hold the output. Is the buffer size adjustable? Like, if you wanted to "cat" a file and then parse the output with TcL? – Cory Nov 10 '14 at 18:04