SSH Expect script
I have this Expect script which called by another script to log Cisco devices. For some reason it has become slow to log into Cisco devices. How may I increase the speed for this script?
#!/usr/bin/expect
log_user 0
set timeout 10
set userid "id"
set password "pass"
# ############## Get two arguments - (1) Device (2) Command to be executed
set device [lindex $argv 0]
set command [lindex $argv 1]
spawn /usr/bin/ssh -l $开发者_JAVA百科userid $device
match_max [expr 32 * 1024]
expect {
-re "RSA key fingerprint" {send "yes\r"}
timeout {puts "Host is known"}
}
expect {
-re "username: " {send "$userid\r"}
-re "(P|p)assword: " {send "$password\r"}
-re "Warning:" {send "$password\r"}
-re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}
-re "Connection closed" {puts "Host error -> $expect_out(buffer)";exit}
-re "no address.*" {puts "Host error -> $expect_out(buffer)";exit}
timeout {puts "Timeout error. Is device down or unreachable?? ssh_expect";exit}
}
expect {
-re "\[#>]$" {send "term len 0\r"}
timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}
expect {
-re "\[#>]$" {send "$command\r"}
timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}
expect -re "\[#>]$"
set output $expect_out(buffer)
send "exit\r"
puts "$output\r\n"
Add exp_internal 1
to your Expect script. You'll be able to see how Expect is matching the patterns. I suspect that your script happens to be pausing for $timeout seconds because one of your patterns failed to match.
I'd first make sure the servers or devices hosting the SSH daemon are not running low on entropy, and I'd use Wireshark or similar to analyze the traffic to see which side of the connection seems to be waiting for the other.
精彩评论