cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
892
Views
5
Helpful
2
Replies

Expect scripting problems

deehusky79
Level 1
Level 1

Hi, I'm a newbie in expect. I need a help to figure out couple things with this script:

 

#Specify all our commands in a list, that we will issue one by one at a later time.
set commands [list "conf t" "do sh ver | i uptime" "do sh clock" "end"]

 

#set prompt "#"
set prompt {([#>]) ?$}

 

foreach device $device_list {
        # Set each device's log file to be the name of the device...
        set file_name $device
        set timeout 30

        # we initiate the login connection
        spawn ssh $device
        match_max [expr 32 * 1024]
        expect -re $prompt
        send \r
        expect -re $prompt

        foreach cmd $commands {
                send "$cmd\r"
                expect -re $prompt
                send \r
                expect -re $prompt
                set output $expect_out(buffer)
                set fd [open $file_name.txt a+]
                puts $fd $output

                close $fd
                }
expect -re $prompt
send "exit\r"

exp_close -i $spawn_id
exp_wait
}

 

My problems are:

1. This script runs the commands but the output get altered, eg:

 

INPUT

R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#
R1(config)#do sh ver | i uptime
R1 uptime is 5 days, 5 hours, 56 minutes
R1(config)#
R1(config)#do sh clock
15:27:41.082 NZST Mon May 14 2018
R1(config)#
R1(config)#end

 

OUTPUT:

conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#
do sh ver | i uptime
R1 uptime is 5 days, 5 hours, 56 minutes
R1(config)#
R1(config)#
R1(config)#

 

MISSING: do sh clock

 

2. How to format the output as per below (the device hostname shows only on the first line).

R1#

conf t
do sh ver | i uptime
R1 uptime is 5 days, 5 hours, 56 minutes
do sh clock
15:27:41.082 NZST Mon May 14 2018
end

 

3. How to save the output into 1 file only, eg:

~$more file_name.txt

R1#

conf t
do sh ver | i uptime
R1 uptime is 5 days, 5 hours, 56 minutes
do sh clock
15:27:41.082 NZST Mon May 14 2018
end

 

R2#

conf t
do sh ver | i uptime
R2 uptime is 6 days, 5 hours, 56 minutes
do sh clock
18:27:41.082 NZST Mon May 14 2018
end

 

R3#

conf t
do sh ver | i uptime
R3 uptime is 7 days, 5 hours, 56 minutes
do sh clock
18:27:41.082 NZST Mon May 14 2018
end

 

Thanks heaps for your time to help me!

2 Replies 2

Joe Clarke
Cisco Employee
Cisco Employee

I'm not sure why you go into config mode to execute show commands.  That seems needlessly complicated.  This version should be closer to what you want.

#Specify all our commands in a list, that we will issue one by one at a later time.
set commands [list "conf t" "do sh ver | i uptime" "do sh clock" "end"]

 

#set prompt "#"
set prompt {([#>]) ?$}

set file_name {file_name.txt}

set fd [open $file_name w]

foreach device $device_list {
        # Set each device's log file to be the name of the device...
        set timeout 30
        puts $fd $device

        # we initiate the login connection
        spawn ssh $device
        match_max [expr 32 * 1024]
        expect -re $prompt

        foreach cmd $commands {
                send "$cmd\r"
                expect -re $prompt
                set output $expect_out(buffer)
                puts $fd $output

        }
        send "exit\r"

        exp_close -i $spawn_id
        exp_wait
}

close $fd

Hi Joe,

Thanks for your time to look at my issue. I use sh commands just for testing. I run your script and some devices missing the last command "end'. Here's the input sample (R2):

 

spawn ssh R1

R1>conf t
en
Password:
R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#do sh ver | i uptime
R1 uptime is 1 week, 3 days, 59 minutes
R1(config)#do sh clock
10:20:34.239 NZST Tue May 15 2018
R1(config)#end
R1#

 

spawn ssh R2

R2>conf t
en
Password:
R2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R2(config)#do sh ver | i uptime
R2 uptime is 1 hour, 48 minutes
R2(config)#do sh clock
10:20:40.020 NZST Tue May 15 2018
R2(config)#

 

spawn ssh R3

.......

 

Could you also explain to me what the purpose of match_max [expr 32 * 1024] please. Can I expect the buffer is big enough to copy the output of 800+ devices?

 

Thanks Joe!

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: