cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
9567
Views
0
Helpful
22
Replies

Expect scripting issue

pantelis1
Level 1
Level 1

Hi All

I am in the process of creating an expect script to capture the output of show commands from various devices and output them as <name of the device>.<command>.txt

However, I am having the following issues:

  1. The "show commands" on the script are not always parsed correctly on the routers (for example the "show ip route" might be parsed as "how ip route" and therefore generate an error)
  2. The output file has the correct format but there is no output within the file

Could you please review and assist in the issues above?

#!/usr/bin/expect 
# Here, we specify all our commands in a list, that we will issue one
# by one at a later time.
set commands {
"show ip route"
"show ip int brief"
"show arp"
}
# Set the date.
set date [timestamp -format %C%y%m%d]
# This variable is for a file called *.txt that has the IP
# of all of the routers you are collecting information from.
set device_list [read [open "routers.txt"]]
# Specify password, as well as what we expect the routers'
# prompt to be.
set pass "*********"
set prompt "#"
# This command tells expect not to echo the output to the console.
exp_log_user 0
# We loop through each device in our list, one by one...
foreach device $device_list {
# Set each device's log file to be the name of the device...
set file_name "$device.txt"

# we initiate the SSH connection
spawn ssh $device -l *********


#If we see a message asking about the device's host key, accept it.
expect -re "Password:" {
exp_send "$pass\r"
}

#exp_send "terminal length 0\n"
foreach cmd $commands {
exp_send "terminal length 0\n"
expect -re $prompt {
exp_log_file -a $file_name.$cmd
exp_send "$cmd\r"
#exp_sleep 2
exp_log_file
}


}
#expect -re $prompt {
#send "term len 0\r"
#}


expect -re $prompt {
exp_send "logout\r"
}


# Turn off logging.
exp_log_file
}
close

Thanks

Pantelis      

22 Replies 22

This is what I meant by adding the close:

#!/usr/bin/expect

# Here, we specify all our commands in a list, that we will issue one

# by one at a later time.

set commands [list "sh ip route summary" "show interfaces" "show arp" "sh ip ospf neighbor" "sh ip eigrp neighbor" "sh cdp neighbors"]

# Set the date.

set date [timestamp -format %C%y%m%d]

# This variable is for a file called switches.txt that has the hostname/IP

# of all of the routers you are collecting information from.tepad

set device_list [read [open "switches.txt"]]

# Specify password, as well as what we expect the switch

# prompt to be.

set pass "********"

#set prompt "#"

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

# This command tells expect not to echo the output to the console.

#exp_log_user 0

#log_user 1

# We loop through each device in our list, one by one...

foreach device $device_list {

# Set each device's log file to be the name of the device...

set file_name $device

# we initiate the SSH connection

eval spawn ssh $device -l ******

match_max [expr 32 * 1024]

#If we see a message asking about the device's host key, accept it.

interact -o -nobuffer -re "assword: $" return

send "$pass\r"

# We log our output from each router to its specified file.

#exp_log_file -a $file_name.$date

# Loop through each command that we specified earlier.

expect -re $prompt

send "term len 0\r"

expect -re $prompt

foreach cmd $commands {

send "$cmd\r"

expect -re $prompt

#remove zeros from the cmd filename

regsub -all {[ \r\t\n]+} $cmd "" correct_cmd

set output $expect_out(buffer)

set fd [open $file_name-$date-$correct_cmd.txt w]

puts $fd $output

close $fd

}

expect -re $prompt

send "exit\r"

close

}

same issue Joseph. The host is been used as a hop to ssh to the next one

#spawn ssh 10.x.xx.xx -l *****

Hi, instead of close, do:

exp_close -i $spawn_id

exp_wait

same issue

code has been amended to :

expect -re $prompt

send "exit\r"

exp_close -i $spawn_id

foreach cmd $commands {

send "$cmd\r"

expect -re $prompt

#remove zeros from the cmd filename

regsub -all {[ \r\t\n]+} $cmd "" correct_cmd

set output $expect_out(buffer)

set fd [open $file_name-$date-$correct_cmd.txt w]

puts $fd $output

close $fd

}

expect -re $prompt

send "exit\r"

exp_close -i $spawn_id

exp_wait

}

Could you post the full version of the current code you have (without any sensivite information of course..)?

Hi Toni

#!/usr/bin/expect

# Here, we specify all our commands in a list, that we will issue one

# by one at a later time.

set commands [list "sh ip route summary" "show interfaces" "show arp" "sh ip ospf neighbor" "sh ip eigrp neighbor" "sh cdp neighbors"]

# Set the date.

set date [timestamp -format %C%y%m%d]

# This variable is for a file called switches.txt that has the hostname/IP

# of all of the switches you are collecting information from.tepad

set device_list [read [open "switches.txt"]]

# Specify the username and password, as well as what we expect the routers'

# prompt to be.

set pass "*******"

#set prompt "#"

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

# This command tells expect not to echo the output to the console.

#exp_log_user 0

#log_user 1

# We loop through each device in our list, one by one...

foreach device $device_list {

# Set each device's log file to be the name of the device...

set file_name $device

# we initiate the SSH connection

eval spawn ssh $device -l *****

match_max [expr 32 * 1024]

#If we see a message asking about the device's host key, accept it.

interact -o -nobuffer -re "assword: $" return

send "$pass\r"

# We log our output from each router to its specified file.

#exp_log_file -a $file_name.$date

# Loop through each command that we specified earlier.

expect -re $prompt

send "term len 0\r"

expect -re $prompt

foreach cmd $commands {

send "$cmd\r"

expect -re $prompt

#remove zeros from the cmd filename

regsub -all {[ \r\t\n]+} $cmd "" correct_cmd

set output $expect_out(buffer)

set fd [open $file_name-$date-$correct_cmd.txt w]

puts $fd $output

close $fd

}

expect -re $prompt

send "exit\r"

exp_close -i $spawn_id

exp_wait

}

Thanks

Pantelis

Hi,

I tried your code and it appears like the host #1 is used to connect to the host #2. BUT it only appears like it because:

#

In reality the connection to host #2 does not originate from the host #1 but from the host you are running the script from.

To clarify, IOS doesn't have a command named "spawn"... so the command would simply fail if you were trying to connect to the host #2 from host #1 using that command.

The code you supplied works for me, all I did was to change the username and password, the code successfully creates files for both the devices I had in the txt-file for the supplied show-commands.

If your experiences differ from mine, I'd next check that you are running the latest versions of expect etc.

Hi Toni

My only concern was the fact that I could see # when running the command. However, it does make sense as the IOS doesn't know what "spawn" is.

Thanks

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: