SSH 期望脚本

发布于 2024-10-22 02:57:29 字数 1309 浏览 2 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-10-29 02:57:29

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.

不交电费瞎发啥光 2024-10-29 02:57:29

我首先确保托管 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文