Cisco Expect 脚本中的错误

Error in Cisco Expect script

我写了一个基本的 Cisco expect 脚本。 ssh 连接后,我想在从文件发送命令行时检测 Cisco 错误输出,例如:

SPAIN#sow crypto isakmp sa
       ^
% Invalid input detected at '^' marker.

我想在循环中捕获“% Invalid”并停止程序。

foreach line $data {
    expect "*#"
    send "$line\r"
    expect {
        "% Invalid*" { send_user "\n Command [ $line ] Failed. \n"; exit 252 }
    }

我在输出中得到这些错误:

SPAIN>enable
Password:
SPAIN#sow crypto isakmp sa
   ^
% Invalid input detected at '^' marker.

SPAIN#invalid command name "sow crypto isakmp sa"
while executing
"$line "
invoked from within
"expect {
     "% Invalid*" { send_user "\n Command [ $line ]  Failed. \n"; exit 252}
}"
("foreach" body line 4)
invoked from within
"foreach line $data {
    expect "*#"
    send "$line\r"
    expect {
        "% Invalid*" { send_user "\n Command [ $line ] Failed. \n"; exit 252}
    }
}"

谢谢

问题是[]在Tcl中有特殊含义;执行命令并 return 结果。

改变这个:

expect {
    "% Invalid*" { send_user "\n Command [ $line ] Failed. \n"; exit 252 }
}

收件人:

expect {
    "% Invalid*" { send_user "\n Command \[ $line \] Failed. \n"; exit 252 }
}

有关详细信息,请参阅 Tcl 手册中的 Evaluation & Substitutions 3: Grouping arguments with []