3

我正在尝试运行脚本以使用期望连接到 Procurve 4204vl 交换机。这是我创建的代码:

#!/usr/bin/expect -f
set timeout 20
spawn ssh -l user 192.168.0.10 
expect "user@192.168.0.10's password:"
send "1234"
send "\r"
expect "Press any key to continue" 
send "j\r"
send "conf"
send "\r"
send "no ip route 89.19.238.2 255.255.255.255 192.168.0.12"
send "\r"
send "exit"
send "\r"
send "exit"
send "\r"
send "exit"
send "\r"
expect "Do you want to log out [y/n]?"
send "y"

我使用 simple 运行它expect script.exp,问题是我得到了这些错误:

  1. 路线没有被删除
  2. 脚本执行完成后,屏幕上出现以下错误:

    按任意键继续执行无效命令名称“y/n”,同时执行从“期望“您要注销 [y/n]?”(文件“script.exp”第 19 行)中调用的“y/n”

那么,我该如何解决这个问题呢?谢谢你。

PS:如果我评论所有"exit"行以及注销问题,然后在"interact"命令的最后一行添加,脚本可以正常工作。

4

2 回答 2

2

对于未删除的路线,程序给你什么输出?您是否看到路由器的任何错误?

在 expect 和 Tcl 中,方括号是执行命令的语法,很像 shell 中的反引号。最简单的解决方案是使用大括号而不是双引号来防止命令插值:

expect {Do you want to log out [y/n]?}

大括号就像 shell 中的单引号一样。

于 2012-12-12T15:47:37.190 回答
0
send "logout\r"
expect {
    "Do you want to log out" {
        send "yy"
        exp_continue
    } "Do you want to save current configuration" {
        set result $expect_out(0,string);
        puts "save..."
        send "y"
        puts "ok"
    } eof {
        puts "end of script"
    }
}
于 2015-10-10T10:55:20.317 回答