我一直在尝试在我的 cisco 设备上自动执行一些配置备份,我已经设法完成了完成任务的脚本,但我也在尝试改进它以处理错误。
我认为有必要在两个步骤中捕获错误,首先是在 'send \"$pass\r\"' 之后获取登录错误(访问被拒绝消息)和在 'expect \": end\"' 行,确保发出的命令能够从设备中提取配置。
如果您使用期望脚本,我已经看到了一些方法,但我想使用 bash 脚本来提供来自 .txt 文件的设备列表。
#!/bin/bash
data=$(date +%d-%m-%Y)
dataOntem=$(date +%d-%m-%Y -d "-1 day")
hora=$(date +%d-%m-%Y-%H:%M:%S)
log=/firewall/log/bkpCisco.$data.log
user=MYUSER
pass=MYPASS
for firewall in `cat /firewall/script/firewall.cisco`
do
VAR=$(expect -c "
spawn ssh $user@$firewall
expect \"assword:\"
send \"$pass\r\"
expect \">\"
send \"ena\r\"
expect \"assword:\"
send \"$pass\r\"
expect \"#\"
send \"conf t\r\"
expect \"conf\"
send \"no pager\r\"
send \"sh run\r\"
log_file -noappend /firewall/backup/$firewall.$data.cfg.tmp
expect \": end\"
log_file
send \"pager 24\r\"
send \"exit\r\"
send \"exit\r\"
")
echo "$VAR"
done