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.