ssh期待脚本

时间:2011-03-15 01:37:58

标签: ssh expect

我有这个期望脚本,由另一个脚本调用来记录cisco设备。某些原因变得很慢,无法登录到cisco设备。是否有人可以看到任何问题或建议可能会提高此脚本的速度。提前谢谢

#!/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 $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"

2 个答案:

答案 0 :(得分:1)

exp_internal 1添加到您的预期脚本中。您将能够看到期望与模式匹配的程度。我怀疑你的脚本碰巧暂停$ timeout秒,因为你的一个模式无法匹配。

答案 1 :(得分:0)

在ServerFault上可能会提出更好的要求。无论如何,我首先要确保托管ssh守护程序的服务器或设备的熵不低,我会使用Wireshark或类似的方法分析流量,看看连接的哪一侧似乎在等待另一端。