从 bash 脚本调用的 Expect 命令内的条件语句

发布于 2024-08-09 03:31:08 字数 890 浏览 9 评论 0原文

我一直在尝试在我的思科设备上自动执行一些配置备份,我已经设法编写完成该任务的脚本,但我也在尝试改进它以处理错误。

我认为有必要分两个步骤捕获错误,首先是在“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-08-16 03:31:08

您需要在期望语句中捕获错误的替代模式。如果您正在寻找特定的错误消息,您可以指定它,或者只指定一个超时处理程序,该处理程序最终将在正常输出无法出现时触发。

例如。在 发送 \"$pass\r\" 而不是 expect \">\" 后尝试:

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

即。如果预期输出在超时(默认 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 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.

初吻给了烟 2024-08-16 03:31:08

如果不使用单引号 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文