Cisco Expect脚本出错

时间:2016-07-09 08:46:52

标签: bash tcl expect cisco

我写了一个基本的思科期望脚本。在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}
    }
}"

感谢

1 个答案:

答案 0 :(得分:0)

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

改变这个:

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 []