思科交换机配置自动备份至文件服务器的回车交互问题求助
交换机/路由器TCL自动备份脚本的回车确认问题
问题描述
在隔离网络的安全环境中,需用TCL脚本实现交换机/路由器配置的每周自动备份至文件服务器,传输前要修改文件名。当前脚本流程为:
- 将运行配置保存至闪存
- 重命名文件(添加日期后缀)
- 删除原文件
- 通过SCP传输至文件服务器
- 删除闪存中的目标文件
但执行copy run $source_file时,系统要求手动按回车确认,尝试添加\r或\r\n会被当作文件名的一部分,无法自动完成流程。
当前脚本代码:
set source_file "sourcefile.txt" set date [clock format [clock seconds] -format "%Y%m%d"] set destination_file "$source_file-$date" set username "username" set password "password" copy run $source_file copy $source_file $destination_file delete /force $source_file copy $destination_file scp://$username:$password@x.x.x.x//path/to/fileshare/$destination_file delete /force $destination_file
解决方法
在脚本开头添加file prompt quiet命令,该命令会关闭Cisco IOS文件操作时的交互确认提示,让copy命令直接执行而无需手动回车。
修改后的完整脚本:
set source_file "sourcefile.txt" set date [clock format [clock seconds] -format "%Y%m%d"] set destination_file "$source_file-$date" set username "username" set password "password" # 关闭文件操作的交互确认提示 file prompt quiet copy run $source_file copy $source_file $destination_file delete /force $source_file copy $destination_file scp://$username:$password@x.x.x.x//path/to/fileshare/$destination_file delete /force $destination_file # 可选:恢复默认的提示行为(如果后续需要交互操作) # file prompt alert
补充说明
file prompt quiet会对所有后续文件操作生效,包括copy、delete等命令,无需再处理交互输入- 如果脚本执行完后需要恢复默认的交互提示,可以在脚本末尾添加
file prompt alert命令
内容的提问来源于stack exchange,提问作者Kenchiken
相关产品推荐
相关产品推荐

