有条件statament内期望命令从一个bash脚本调用 [英] Conditional statament inside expect command called from a bash script

查看:281
本文介绍了有条件statament内期望命令从一个bash脚本调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想在我的Cisco设备自动执行某些配置备份,我已经成功地做​​到这一点完成该任务的脚本,但我试图改善它处理错误了。

我觉得这是必要的捕捉错误的两个步骤,先刚过发送\\$通\\ r \\'来获得登录错误(拒绝访问的消息),并在预期\\:到底\\ 行,以确保发出的命令能够从设备拉出配置。

我见过的一些方法,如果你在一个expect脚本工作,做到这一点,但我想用一个bash脚本,以便能够从.txt文件中提供的设备列表。

 #!/斌/庆典数据= $(日期+%D-%M-%Y)
dataOntem = $(日期+%D-%M-%Y -d-1天)
霍拉= $(日期+%D-%M-%Y-%H:%M:%S)
登录= /防火墙/日志/ bkpCisco。$ data.log
用户= MYUSER
通过=将mypass在`猫/防火墙/脚本/ firewall.cisco`防火墙
做VAR = $(预计-c
产卵SSH用户$ @ $防火墙
预计\\assword:\\
发送\\$通\\ r \\
预计\\> \\
发送\\ENA \\ r \\
预计\\assword:\\
发送\\$通\\ r \\
预计\\#\\
发送\\的conf牛逼\\ r \\
预计\\的conf \\
发送\\无寻呼机\\ r \\
发送\\嘘运行\\ r \\
LOG_FILE -noappend /firewall/backup/$firewall.$data.cfg.tmp
预计\\:到底\\
LOG_FILE
发送\\呼机24 \\ r \\
发送\\退出\\ r \\
发送\\退出\\ r \\
)回声$ VAR
DONE


解决方案

您需要在您想要捕获错误的期望陈述的替代模式。如果你正在寻找一个特定的错误消息,您可以指定,或者只是指定超时处理程序时,正常输出没有出现,这将最终触发。

例如。在发送\\$通\\ r \\而不是预计\\> \\尝试:

 预计\\> \\{} {超时标准错误放在{无法登录};出口}

IE浏览器。如果预期输出的超时时间(默认为10秒)到达之前什么也不做,继续执行,否则抱怨和退出的期望。您可能还需要一个EOF模式在您的SSH会话结束的情况相符。
需要注意的是,因为你的期望没有做任何变量替换,你不需要\\\\在你的字符串,你可以使用{}甚至没有当它是一个字,如: 期望的conf 发送{没有寻呼机}

BTW我同意bstpierre,如果你掉bash和的确在整个事情,这将是更清洁的期望,但如果bash所做的这是确定的工作。

I've been trying to automate some configuration backups on my cisco devices, i've already managed to do the script that accomplishes the task but I'm trying to improve it to handle errors too.

I think that's necessary to catch the errors on two steps, first just after the 'send \"$pass\r\"' to get login errors (access denied messages) and at the 'expect \": end\"' line, to be sure that the commands issued were able to pull the configuration from the device.

I've seen some ways to do it if you work on a expect script, but i want to use a bash script to be able to supply a list of devices from a .txt file.

#!/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

解决方案

You need alternative patterns in the expect statements where you want to catch errors. If you're looking for a specific error message you can specify that, alternatively just specify a timeout handler which will eventually trigger when the normal output fails to appear.

Eg. after send \"$pass\r\" instead of expect \">\" try:

expect \">\" {} timeout {puts stderr {Could not log in}; exit}

ie. if the expected output arrives before the timeout (default 10 sec) do nothing and continue, otherwise complain and exit from expect. You might also need an eof pattern to match the case where your ssh session ends. Note that since you don't do any variable substitution in expect, you don't need \"\" around your strings, you can use {} or even nothing when it's one word, eg. expect conf and send {no pager}.

BTW I agree with bstpierre that this would be cleaner if you dropped bash and did the whole thing in expect, but if bash does the job that's ok.

这篇关于有条件statament内期望命令从一个bash脚本调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆