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"
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 $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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
exp_internal 1
添加到您的 Expect 脚本中。您将能够看到 Expect 如何匹配模式。我怀疑您的脚本恰好暂停 $timeout 秒,因为您的模式之一无法匹配。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.我首先确保托管 SSH 守护进程的服务器或设备的熵运行不低,并且我会使用 Wireshark 或类似工具来分析流量,以查看连接的哪一端似乎正在等待另一端。
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.