SSD固件更新计时脚本中Expect/Send失效,卡在首个提示符
解决Expect脚本卡在fumagician交互提示符的问题
问题原因
你的脚本卡在第一个交互提示符,核心是两个常见问题:
- 精确匹配的语法错误:
expect -ex {"Press any key to continue..."}里的外层大括号+双引号会干扰Expect的精确匹配逻辑,导致无法识别目标提示符。 - 未处理终端隐藏字符:fumagician的输出可能包含ANSI控制符(如颜色、光标移动代码)或换行/回车组合,导致精确匹配失败。
修复步骤
1. 修正匹配语法
去掉-ex参数后的外层引号,直接使用字符串匹配。如果需要兼容更多输出格式,可以改用正则表达式模式(-re)。
2. 调试确认真实输出
先通过调试模式运行脚本,查看fumagician的实际输出细节:
expect -d your_script.exp
这会打印Expect的详细交互日志,你能看到程序实际输出的所有字符(包括隐藏控制符),从而精准调整匹配规则。
3. 修改后的可用脚本
#!/usr/bin/expect -f set timeout -1 cd /tmp/fwupdate cd root/fumagician/ # 禁用终端颜色输出,避免控制符干扰匹配 spawn env TERM=dumb ./fumagician # 用正则匹配兼容可能的输出差异 expect -re "Press any key to continue..." send "\n" expect -re "Press any key to continue..." send "\n" # 正则中需要转义方括号 expect -re "Do you want to continue the firmware update? \\[Y/N\\]: " send "Y\n" set start_time [clock seconds] expect -re "Do you want to continue the firmware update? \\[Y/N\\]: " send "Y\n" expect -re "Firmware Update Completed" set end_time [clock seconds] set elapsed_time [expr $end_time - $start_time] expect -re "Press any key to EXIT..." send "\n" puts "FW Update Time: $elapsed_time seconds"
额外优化提示
- 若
\n无法触发响应,可尝试替换为\r,不同工具对换行符的要求可能不同。 - 如果仍有匹配问题,可缩短匹配字符串(比如只匹配
Press any key),进一步降低匹配门槛。
内容的提问来源于stack exchange,提问作者hacker_man_2021
相关产品推荐
相关产品推荐

