从 bash 脚本调用的 Expect 命令内的条件语句
我一直在尝试在我的思科设备上自动执行一些配置备份,我已经设法编写完成该任务的脚本,但我也在尝试改进它以处理错误。
我认为有必要分两个步骤捕获错误,首先是在“send \"$pass\r\””之后获取登录错误(访问被拒绝消息),然后在“expect \”: end\””行,确保发出的命令能够从设备中提取配置。
如果您使用 Expect 脚本,我已经看到了一些方法可以做到这一点,但我想使用 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在期望语句中捕获错误的替代模式。如果您正在寻找特定的错误消息,您可以指定它,或者只指定一个超时处理程序,该处理程序最终将在正常输出无法出现时触发。
例如。在
发送 \"$pass\r\"
而不是expect \">\"
后尝试:即。如果预期输出在超时(默认 10 秒)之前到达,则不执行任何操作并继续,否则抱怨并退出预期。您可能还需要一个 eof 模式来匹配 ssh 会话结束的情况。
请注意,由于您在expect中没有进行任何变量替换,因此您不需要在字符串周围使用\"\",因此您可以使用{},甚至当它是一个单词时什么也不用,例如。
期待conf
并发送{无寻呼机}
。顺便说一句,我同意 bstpierre 的观点,如果你放弃 bash 并按照预期完成所有事情,那么这会更干净,但如果 bash 完成了这项工作,那就没问题了。
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 ofexpect \">\"
try: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
andsend {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.
如果不使用单引号 (
expect -c '...'
),则所有 $variables 都将被 bash notexpect 替换。将期望代码放在单独的文件中可能更容易,或者可能是定界文档。If you don't use single quotes (
expect -c '...'
), then all the $variables will be substituted by bash not expect. May be easier to put the expect code in a separate file, or maybe a heredoc.