我正在编写一个脚本, expect通过ssh自动configurationcisco路由器。 这个脚本的一部分是将图像复制到flash如果它不存在:
对于这个期望发送一个copy并等待Destination filename然后发送一个return 。
cisco#copy tftp://10.xx.xx.3/name_of_image.bin flash: Destination filename [name_of_image.bin]?
现在有两个select:
flash: 如果文件已经存在,它看起来像这样:
%Warning:There is a file already existing with this name Do you want to over write? [confirm]
现在到我的问题:脚本等待Do you want to over write? [confirm] Do you want to over write? [confirm] ,然后应该退出,因为图像已经在闪光。 但目前似乎expect无法识别该行,并被卡在确认。 它已经尝试了几个模式,如[confirm] , *[confirm]* , Do , *write*等等。
我目前的代码看起来像这样:
expect "*#" send "copy tftp://10.$ip_local.3/name_of_image.bin flash:\r" expect "Destination filename" send "\r" expect { "Do you want to over write? [confirm]"{ interact abort } expect "*#" send "exit\r"
我究竟做错了什么?
有几件事是错误的:
[confirm]将尝试调用期望命令“确认” – 方括号期望是像反引号在壳 expect命令的多模式forms,其中第一个匹配成功。 尝试这个:
expect { -exact {Do you want to over write? [confirm]} { send "\r" exp_continue } "*#" } send "exit\r" expect eof
笔记
? 并会干涉。 [confirm]尝试命令replace exp_continue保持在expect命令中。 这意味着我仍然可以查找命令提示符。 exit ,等待eof ,让ssh会话正常结束