【问题标题】:Last expect instruction is not executed最后的期望指令未执行
【发布时间】:2016-05-31 15:17:04
【问题描述】:

我正在编写一个脚本,使用 expect 命令自动执行 IOS Cisco 路由器上的指令。

这是我写的脚本:

#!/bin/expect
#log_user 0
set timeout 60

set mode [lindex $argv 0]
spawn ssh myuser@10.0.0.254


expect "*assword:" {
    send "mypassword\r"
}

expect "*>" {
    send "enable \r"
}

expect "*#" {
    send "conf t \r"
}

expect "*(config)" {
        send "interface Serial0/0/0 \r"
        send "clock rate 14400\r"
}

我的问题是,这个脚本停在“router1(config)#”状态。 但是,如果我在脚本末尾添加一个interact 命令,那么所有这些都可以正常工作。用户正确进入界面配置模式,时钟频率更新良好。 但事实是,我不希望用户进行交互。

所以我真的不明白发生了什么以及为什么我不能就这样结束脚本......

如果你有任何线索...?

【问题讨论】:

  • 使用expect -d 运行您的脚本并检查调试输出以查看您的模式是否匹配。

标签: linux bash expect cisco-ios


【解决方案1】:

我终于自己解决了我的问题。

这有点作弊,但是……很好用。

鉴于这是最后一个不起作用的 expect 命令,我在脚本末尾添加了最后一个“无用的期望”:

expect "*(config)" {
        send "interface Serial0/0/0 \r"
}
expect "*(config-if)" {
        send "clock rate 14400\r"
}
expect "*(config-if)" {
        send "end"
        close
}

无论如何,如果有人有正确的方法,我仍然开放

【讨论】:

  • 这正是问题所在。您的 send 太快,没有等待ssh 进程退出
  • 是的,但即使我在脚本末尾有一个 sleep 5,它仍然不起作用...
猜你喜欢
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
相关资源
最近更新 更多