10-15-2013 03:32 AM
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:
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
Solved! Go to Solution.
10-18-2013 05:56 PM
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
}
10-21-2013 01:14 AM
same issue Joseph. The host is been used as a hop to ssh to the next one
10-21-2013 03:28 AM
Hi, instead of close, do:
exp_close -i $spawn_id
exp_wait
10-21-2013 03:31 AM
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
}
10-21-2013 03:44 AM
Could you post the full version of the current code you have (without any sensivite information of course..)?
10-21-2013 03:48 AM
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
10-21-2013 04:32 AM
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.
10-21-2013 04:52 AM
Hi Toni
My only concern was the fact that I could see
Thanks
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide